java programming language mr.rungrote phonkam...

51
5 5 Java Programming Language Mr.Rungrote Phonkam [email protected]

Upload: vuongtruc

Post on 15-May-2018

237 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

55

Java Programming LanguageMr.Rungrote [email protected]

Page 2: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

ContentsContents

1. Expressions2. Control Flow

2.1 Condition2.2 Multiple Branch2.3 Lopps2.4 Jump

1. Expressions2. Control Flow

2.1 Condition2.2 Multiple Branch2.3 Lopps2.4 Jump

Java Programming Language by Rungrote Phonkam

Page 3: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

1. Expressions1. Expressions1. Expressions

�����������

� !���1���������� ������������ ����� ��� �������� ��������������� !"!#��$��

"!!%�&�

� ����$��#%'��������(!�)���#�� �% *� *!��% �+� � ��

����'$� 5 / b % 2 + 10;(a < b) && (c > d);

5 * 800;a = 12.5;

����������� �����������

� !���1���������� ������������ ����� ��� �������� ��������������� !"!#��$��

"!!%�&�

� ����$��#%'��������(!�)���#�� �% *� *!��% �+� � ��

����'$� 5 / b % 2 + 10;(a < b) && (c > d);

5 * 800;a = 12.5;

Java Programming Language by Rungrote Phonkam

Page 4: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

Java Programming Language by Rungrote Phonkam

����������� � ��� ����� � ���

Postfix operators [] . (params) expr++ expr--

Unary operators ++expr --xpr +expr -expr ~ !

Creation or cast new (type)expr

Multiplicative * / %

additive + -

shift << >> >>>

relational < > <= >= instanceof

equality == !=

bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

logical AND &&

Logical OR ||

Conditional ? :

Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Page 5: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

1. Expressions1. Expressions1. Expressions���� a + b � 100 * 2 / 5

������� a = 45 *!� b = 20

�%' �� ���+� � ����� ((a + b) � ((100 * 2) / 5))

1: a + b � (200) / 5

2: a + b � (50)

3: (65) � 50

4: 15

���� --a % b++ * 15

������� a = 3 *!� b = 1

�%' �� ���+� � ����� --a % b++ * 15

1: (--3) % (1++) * 15

2: 2 % 2 * 15

3: 0 * 15

4: 0

���� a + b � 100 * 2 / 5

������� a = 45 *!� b = 20

�%' �� ���+� � ����� ((a + b) � ((100 * 2) / 5))

1: a + b � (200) / 5

2: a + b � (50)

3: (65) � 50

4: 15

���� --a % b++ * 15

������� a = 3 *!� b = 1

�%' �� ���+� � ����� --a % b++ * 15

1: (--3) % (1++) * 15

2: 2 % 2 * 15

3: 0 * 15

4: 0

Java Programming Language by Rungrote Phonkam

Page 6: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

1. Expressions1. Expressions1. Expressions

�����������

� !���1�����-',��

# ���'&% ����-'����+��*���,�����-',���� ����� ����(��-��$������� Instance) ��)��� Object

����'$� �����# 1

Integer.parseInt(“12”);

�����# 2

System.out.println(“Hello Java”);

����������� �����������

� !���1�����-',��

# ���'&% ����-'����+��*���,�����-',���� ����� ����(��-��$������� Instance) ��)��� Object

����'$� �����# 1

Integer.parseInt(“12”);

�����# 2

System.out.println(“Hello Java”);

Java Programming Language by Rungrote Phonkam

Page 7: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2. Control Flows2. Control Flows2. Control Flows�������������� ������������������� �������� (Conditions)

if � else

� �������$� (Multiple Branch)switch .. case

� /01 (Loop)for �

do � while

while �

� ��3455(Jump)break, continue, return

�������������� ������������������� �������������������������������� �������� (Conditions)

if � else

� �������$� (Multiple Branch)switch .. case

� /01 (Loop)for �

do � while

while �

� ��3455(Jump)break, continue, return

Java Programming Language by Rungrote Phonkam

Page 8: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition

������������ �

Expression true false

Statement ��� �� ��$��� ��

������������ �

Expression true false

Statement ��� �� ��$��� ��

Java Programming Language by Rungrote Phonkam

�����

�..

expression

Statement(s)

�..

��

Y

N

Page 9: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition������

if ( expression )

statements

���

if �)��-'��������)#����+���������+���*������#�

expression �)���*������#��-#,������� ��!"��$����� 2�',���'�$1�',�� ��*�

statements �)������# ,������ ��,�. &���� ����)#� expression

�!"� true

������

if ( expression )

statements

���

if �)��-'��������)#����+���������+���*������#�

expression �)���*������#��-#,������� ��!"��$����� 2�',���'�$1�',�� ��*�

statements �)������# ,������ ��,�. &���� ����)#� expression

�!"� true

Java Programming Language by Rungrote Phonkam

Page 10: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass CheckPositiveNumber1{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0)System.out.println("Your Number [" +Integer.parseInt(args[0]) + "] is Positive");

}}

class CheckPositiveNumber1{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0)System.out.println("Your Number [" +Integer.parseInt(args[0]) + "] is Positive");

}}

Java Programming Language by Rungrote Phonkam

java CheckPositiveNumber1 10Your Number [10] is Positive

Page 11: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass CheckPositiveNumber2{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0){ System.out.print(“Your Number [“);

System.out.print(Integer.parseInt(args[0]));System.out.println(“] is Positive”);

}}

}

class CheckPositiveNumber2{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0){ System.out.print(“Your Number [“);

System.out.print(Integer.parseInt(args[0]));System.out.println(“] is Positive”);

}}

}

Java Programming Language by Rungrote Phonkam

java CheckPositiveNumber2 15Your Number [15] is Positive

Page 12: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass CheckPositiveNumber3{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0)System.out.pring(“Your Number [“);System.out.pring(Integer.parseInt(args[0]));System.out.pringln(“] is Positive”);

}}

class CheckPositiveNumber3{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>0)System.out.pring(“Your Number [“);System.out.pring(Integer.parseInt(args[0]));System.out.pringln(“] is Positive”);

}}

Java Programming Language by Rungrote Phonkam

java CheckPositiveNumber3 20Your Number [20] is Positive

Page 13: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition

������������

Expression true false

Statement1 ��� �� ��$��� ��

Statement2 ��$��� �� ��� ��

������������

Expression true false

Statement1 ��� �� ��$��� ��

Statement2 ��$��� �� ��� ��

Java Programming Language by Rungrote Phonkam

�����

…..

expression

Statement1(s)

…..

��

Statement2(s)

YN

Page 14: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition������

if ( expression )

statement1

else statement2

���if �������������� �������������������������� �expression ������������� ��� ��� ����!��"#��$���% &��������$

'�����(����else ������������)��������*�����)�(������)��� (

��� � expression ��� ����!��"#� false

statement1 ��������������� +���)�(����� � expression ��� ����!��"#� true

statement2 ��������������� +���)�(����� � expression ��� ����!��"#� false

������if ( expression )

statement1

else statement2

���if �������������� �������������������������� �expression ������������� ��� ��� ����!��"#��$���% &��������$

'�����(����else ������������)��������*�����)�(������)��� (

��� � expression ��� ����!��"#� false

statement1 ��������������� +���)�(����� � expression ��� ����!��"#� true

statement2 ��������������� +���)�(����� � expression ��� ����!��"#� false

Java Programming Language by Rungrote Phonkam

Page 15: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass YourAge1{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30)System.out.println(“You’re Older”);

elseSystem.out.println(“You’re Younger”);

}}

class YourAge1{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30)System.out.println(“You’re Older”);

elseSystem.out.println(“You’re Younger”);

}}

Java Programming Language by Rungrote Phonkam

java YourAge1 18You're Youngerjava YourAge1 35You're Older

Page 16: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass YourAge2{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30)System.out.println(“You’re Older”);

if (Integer.parseInt(args[0])<30)System.out.println(“You’re Younger”);

}}

class YourAge2{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30)System.out.println(“You’re Older”);

if (Integer.parseInt(args[0])<30)System.out.println(“You’re Younger”);

}}

Java Programming Language by Rungrote Phonkam

java YourAge2 15You're Youngerjava YourAge2 59You're Older

Page 17: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass YourAge3{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30){ System.out.println(“You’re Older”);

System.out.println(“Younger than 30 about “ +(30 - Integer.parseInt(args[0])));

}else

{ System.out.println(“You’re Younger”);System.out.println(“Older than 30 about “ +

(Integer.parseInt(args[0]) – 30));}

}}

class YourAge3{ public static void main(String args[])

{ if (Integer.parseInt(args[0])>30){ System.out.println(“You’re Older”);

System.out.println(“Younger than 30 about “ +(30 - Integer.parseInt(args[0])));

}else

{ System.out.println(“You’re Younger”);System.out.println(“Older than 30 about “ +

(Integer.parseInt(args[0]) – 30));}

}}

Java Programming Language by Rungrote Phonkam

Page 18: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

����������Expression1 true false

Expression1 true false

Statement1 ������ ���

������

Statement2 ��������� ������

����������Expression1Expression1 true false

Expression1Expression1 true false

Statement1Statement1 ������ ���

������

Statement2Statement2 ��������� ������

2.1. Condition2.1. Condition2.1. Condition

Java Programming Language by Rungrote Phonkam

�����

….

Expression1

Statement1

...

��

Statement2

Y N

�����������

Statement3

N

Y

Expression2

Page 19: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition

����������

Expression1 true true false false

Expression2 true false true false

Statement1 ������ ������ ��������� ���������

Statement2 ��������� ��������� ������ ���������

Statement3 ��������� ��������� ��������� ������

����������

Expression1 true true false false

Expression2 true false true false

Statement1 ������ ������ ��������� ���������

Statement2 ��������� ��������� ������ ���������

Statement3 ��������� ��������� ��������� ������

Java Programming Language by Rungrote Phonkam

Page 20: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass CheckUserPassword{ public static void main(String args[])

{ if (arg.length == 2)if (args[0].equals(“JAVA”))

{ if (args[1].equals(“best”))System.out.println(“Corrected USER and

PASSWORD”);}else System.out.println(“Wrong USER and

PASSWORD”);else System.out.println(“Please, Fill parameter USER

and PASSWORD”);}

}

class CheckUserPassword{ public static void main(String args[])

{ if (arg.length == 2)if (args[0].equals(“JAVA”))

{ if (args[1].equals(“best”))System.out.println(“Corrected USER and

PASSWORD”);}else System.out.println(“Wrong USER and

PASSWORD”);else System.out.println(“Please, Fill parameter USER

and PASSWORD”);}

}

Java Programming Language by Rungrote Phonkam

Page 21: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition

Java Programming Language by Rungrote Phonkam

java CheckUserPasswordPlease, Fill parameter USER and PASSWORDjava CheckUserPassword Hi JavaWrong USER and PASSWORDjava CheckUserPassword JAVA BESTCorrected USER and PASSWORD

Page 22: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Conditionclass YourAge4{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);if (a <=0)

System.out.println(“Impossible! Check Your Age”);else if (a <= 10)

System.out.println(“Hi Babe!”);else if (a <= 20)

System.out.println(“Wow! Teeage.”);else if (a <= 30)

System.out.println(“Good!Younger.”);

else if (a >30)System.out.println(“How about

your healthy?”);}

}

class YourAge4{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);if (a <=0)

System.out.println(“Impossible! Check Your Age”);else if (a <= 10)

System.out.println(“Hi Babe!”);else if (a <= 20)

System.out.println(“Wow! Teeage.”);else if (a <= 30)

System.out.println(“Good!Younger.”);

else if (a >30)System.out.println(“How about

your healthy?”);}

} Java Programming Language by Rungrote Phonkam

Page 23: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.1. Condition2.1. Condition2.1. Condition

Java Programming Language by Rungrote Phonkam

java YourAge4 -3Impossible! Check Your Agejava YourAge4 5Hi Babe!java YourAge4 18Wow! Teeage.java YourAge4 25Good! Younger.java YourAge4 31java YourAge4

Page 24: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branch

������ switch ( Variable ){ case Data_1: statement_1; break;

case Data_2: statement_2; break;…case Data_n: statement_n; break;default: statement;

}

switch �� ������������������������������������� ����������������� ���� �� !�"� #��������� ����$�����

Variable ����� �#�%break ������������ ����������� switch...case ����������case ���������������������������������������������������� !���"#������$"%default ������������������������������ �$������$"%��&��'�($#&���� case ��)

������ switch ( Variable ){ case Data_1: statement_1; break;

case Data_2: statement_2; break;…case Data_n: statement_n; break;default: statement;

}

switch �� ������������������������������������� ����������������� ���� �� !�"� #��������� ����$�����

Variable ����� �#�%break ������������ ����������� switch...case ����������case ���������������������������������������������������� !���"#������$"%default ������������������������������ �$������$"%��&��'�($#&���� case ��)

Java Programming Language by Rungrote Phonkam

Page 25: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branchclass MonthName{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Jan”); break;

case 2: System.out.println(“Feb”); break;case 3: System.out.println(“Mar”); break;case 4: System.out.println(“Apr”); break;case 5: System.out.println(“May”); break;case 6: System.out.println(“Jun”); break;case 7: System.out.println(“Jul”); break;case 8: System.out.println(“Aug”); break;case 9: System.out.println(“Sep”); break;case 10: System.out.println(“Oct”); break;case 11: System.out.println(“Nov”); break;case 12: System.out.println(“Dec”); break;

}}

}

class MonthName{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Jan”); break;

case 2: System.out.println(“Feb”); break;case 3: System.out.println(“Mar”); break;case 4: System.out.println(“Apr”); break;case 5: System.out.println(“May”); break;case 6: System.out.println(“Jun”); break;case 7: System.out.println(“Jul”); break;case 8: System.out.println(“Aug”); break;case 9: System.out.println(“Sep”); break;case 10: System.out.println(“Oct”); break;case 11: System.out.println(“Nov”); break;case 12: System.out.println(“Dec”); break;

}}

}

Java Programming Language by Rungrote Phonkam

Page 26: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branchclass DayName1{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Sun.”);

case 2: System.out.println(“Mon.”);case 3: System.out.println(“Tue.”);case 4: System.out.println(“Wed.”);case 5: System.out.println(“Thu.”);case 6: System.out.println(“Fri.”);case 7: System.out.println(“Sat.”);

}}

}

class DayName1{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Sun.”);

case 2: System.out.println(“Mon.”);case 3: System.out.println(“Tue.”);case 4: System.out.println(“Wed.”);case 5: System.out.println(“Thu.”);case 6: System.out.println(“Fri.”);case 7: System.out.println(“Sat.”);

}}

}

Java Programming Language by Rungrote Phonkam

Page 27: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branch

Java Programming Language by Rungrote Phonkam

DayName1 5ThuFriSat

Page 28: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branchclass DayInMonth{ public static void main(String args[])

{ int month = Integer.parseInt(args[0]);int year = Integer.parseInt(args[1]);int numDays = 0;switch (month){ case 1: case 3: case 5: case 7:

case 8: case 10: case 12:numDays = 31; break;

case 4: case 6: case 9: case 11:numDays = 30; break;

case 2:if ( ((year % 4 == 0) && !(year % 100 == 0)) ||

(year % 400 == 0) )numDays = 29;

else numDays = 28; break;}

System.out.println(numDays);}

}

class DayInMonth{ public static void main(String args[])

{ int month = Integer.parseInt(args[0]);int year = Integer.parseInt(args[1]);int numDays = 0;switch (month){ case 1: case 3: case 5: case 7:

case 8: case 10: case 12:numDays = 31; break;

case 4: case 6: case 9: case 11:numDays = 30; break;

case 2:if ( ((year % 4 == 0) && !(year % 100 == 0)) ||

(year % 400 == 0) )numDays = 29;

else numDays = 28; break;}

System.out.println(numDays);}

}

Java Programming Language by Rungrote Phonkam

Page 29: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.2. Multiple Branch2.2. Multiple Branch2.2. Multiple Branchclass DayName2{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Sun.”); break;

case 2: System.out.println(“Mon.”); break;case 3: System.out.println(“Tue.”); break;case 4: System.out.println(“Wed.”);break;case 5: System.out.println(“Thu.”);break;case 6: System.out.println(“Fri.”); break;case 7: System.out.println(“Sat.”); break;default: System.out.println(“Invalid Number”);

}}

}

class DayName2{ public static void main(String args[])

{ int a = Integer.parseInt(args[0]);switch (a){ case 1: System.out.println(“Sun.”); break;

case 2: System.out.println(“Mon.”); break;case 3: System.out.println(“Tue.”); break;case 4: System.out.println(“Wed.”);break;case 5: System.out.println(“Thu.”);break;case 6: System.out.println(“Fri.”); break;case 7: System.out.println(“Sat.”); break;default: System.out.println(“Invalid Number”);

}}

} Java Programming Language by Rungrote Phonkam

Page 30: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops������

for ( Initial_Counter ; Condition ; Increase_Counter ���Decrease_Counter )

Statements

�����

for ������������ ����� ������� ����������( ; ; ) ������������������������� !��� ������� !���"��!�

��!�� #!���$�����������%���&'� ;Initial_Counter ���������#����(����(�����Condition ���������(����������%"������������Increase_Counter ��� Decrease_Counter

�������%����'�%��#�&����� $����������$�#�'% %��#� ��#� a++ ��� a--��*�&��

������

for ( Initial_Counter ; Condition ; Increase_Counter ���Decrease_Counter )

Statements

�����

for ������������ ����� ������� ����������( ; ; ) ������������������������� !��� ������� !���"��!�

��!�� #!���$�����������%���&'� ;Initial_Counter ���������#����(����(�����Condition ���������(����������%"������������Increase_Counter ��� Decrease_Counter

�������%����'�%��#�&����� $����������$�#�'% %��#� ��#� a++ ��� a--��*�&��

Java Programming Language by Rungrote Phonkam

Page 31: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass PrintWord1{ public static void main(String args[])

{ int a;for(a = 0; a < 10; a++)

System.out.println(“Sawadee”);}

}

class PrintWord1{ public static void main(String args[])

{ int a;for(a = 0; a < 10; a++)

System.out.println(“Sawadee”);}

}

Java Programming Language by Rungrote Phonkam

java PrintWord1Sawadee…Sawadee

Page 32: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass PrintWord2{ public static void main(String args[])

{ for(int a = 0; a < 10; a++){ System.out.print(a);

System.out.println(“ Sawadee”);}

}}

class PrintWord2{ public static void main(String args[])

{ for(int a = 0; a < 10; a++){ System.out.print(a);

System.out.println(“ Sawadee”);}

}}

Java Programming Language by Rungrote Phonkam

java PrintWord20 Sawadee…9 Sawadee

Page 33: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass PrintWord4

{ public static void main(String args[])

{ for(int a = 0; a < args[0].length(); a++)

System.out.println(args[0]);

}

}

class PrintWord4

{ public static void main(String args[])

{ for(int a = 0; a < args[0].length(); a++)

System.out.println(args[0]);

}

}

Java Programming Language by Rungrote Phonkam

Page 34: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops

������

while ( Condition )

Statement

��� ��

Condition ������������� ����������� ������������� ��� ��� ������������ �� ������� true ��� ������������� ������ �����

������

while ( Condition )

Statement

��� ��

Condition ������������� ����������� ������������� ��� ��� ������������ �� ������� true ��� ������������� ������ �����

Java Programming Language by Rungrote Phonkam

Page 35: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops

������

Initial_Counter

while ( Condition ){ Statement

Increase_Counter � �� Decrease_Counter

}

�����

Initial_Counter �������������� ��!�����!� �"��#$%!�� ����"�� for

������

Initial_Counter

while ( Condition ){ Statement

Increase_Counter � �� Decrease_Counter

}

�����

Initial_Counter �������������� ��!�����!� �"��#$%!�� ����"�� for

Java Programming Language by Rungrote Phonkam

Page 36: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass FormularMultiple1{ public static void main(String args[])

{ int a = 1; // Initial Counterwhile (a <= 12) // Counter Check{ System.out.print( a + “ x “ + args[0] + “ = “);

System.out.println(Integer.parseInt(args[0]) * a);a++; // Increase Counter

}}

}

class FormularMultiple1{ public static void main(String args[])

{ int a = 1; // Initial Counterwhile (a <= 12) // Counter Check{ System.out.print( a + “ x “ + args[0] + “ = “);

System.out.println(Integer.parseInt(args[0]) * a);a++; // Increase Counter

}}

}

Java Programming Language by Rungrote Phonkam

java FormularMultiple1 51 x 5 = 52 x 5 = 103 x 5 = 15…11 x 5 = 5512 x 5 = 60

Page 37: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass FormularMultiple2{ public static void main(String args[])

{ int a = 1; // Initial Counterwhile (a <= 12){ System.out.print( a + “ x “ + args[0] + “ = “);

System.out.println(Integer.parseInt(args[0]) * a++);}

}}

class FormularMultiple2{ public static void main(String args[])

{ int a = 1; // Initial Counterwhile (a <= 12){ System.out.print( a + “ x “ + args[0] + “ = “);

System.out.println(Integer.parseInt(args[0]) * a++);}

}}

Java Programming Language by Rungrote Phonkam

java FormularMultiple2 31 x 3 = 32 x 3 = 63 x 3 = 9…11 x 3 = 3312 x 3 = 36

Page 38: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass Factorial1{ public static void main(String args[])

{ int fac = 1, val = Integer.parseInt(args[0]);while (val >0)

fac *= val--;System.out.println(fac);

}}

class Factorial1{ public static void main(String args[])

{ int fac = 1, val = Integer.parseInt(args[0]);while (val >0)

fac *= val--;System.out.println(fac);

}}

Java Programming Language by Rungrote Phonkam

java Factorial1 5120

Page 39: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops

������

do

Statementswhile ( Condition );

�����

Condition ��������&'���������'����� �������&'�$��������()����������� ������� true ��������������������%*#�����������$����� false

������

do

Statementswhile ( Condition );

�����

Condition ��������&'���������'����� �������&'�$��������()����������� ������� true ��������������������%*#�����������$����� false

Java Programming Language by Rungrote Phonkam

Page 40: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops

'�����$% ��$% ���!������ while � ��� do � while

��� �������� ���� �� ������������� ����������������

True False

while ��! ����� &������� 0

do�while ��!���� ����� ����� 1

'�����$% ��$% ���!������ while � ��� do � while

��� �������� ���� �� ������������� ����������������

True False

while ��! ����� &������� 0

do�while ��!���� ����� ����� 1

Java Programming Language by Rungrote Phonkam

Page 41: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass DoWhile{ public static void main(String args[])

{ doSystem.out.println(“Don’t forget me”);

while (5>10);}

}

class DoWhile{ public static void main(String args[])

{ doSystem.out.println(“Don’t forget me”);

while (5>10);}

}

Java Programming Language by Rungrote Phonkam

java DoWhileDon’t forget me

Page 42: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass BarChart{ public static void main(String args[])

{ for (int a = 0; a < arg.length; a++){ for(int b=0; b<Integer.parseInt(args[a]); b++)

System.out.print(“*”);System.out.print(“\n”);

}}

}

class BarChart{ public static void main(String args[])

{ for (int a = 0; a < arg.length; a++){ for(int b=0; b<Integer.parseInt(args[a]); b++)

System.out.print(“*”);System.out.print(“\n”);

}}

}

Java Programming Language by Rungrote Phonkam

java BarChart 3 7 5***************

Page 43: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loopsclass InfinityLoop{ public static void main(String args[])

{ for (int a = 1; a > 0; a++)System.out.print(“I\’ll be Back\t”);

}}

class InfinityLoop{ public static void main(String args[])

{ for (int a = 1; a > 0; a++)System.out.print(“I\’ll be Back\t”);

}}

Java Programming Language by Rungrote Phonkam

java InfinityLoopI’ll be Back I’ll be Back I’ll be Back I’ll be Back …

Page 44: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.3. Loops2.3. Loops2.3. Loops

Infinity Loop

� �$� 1 for ( ; ; ) ;

� �$� 2 while (true) ;

� �$� 3 do { } while (true);

Infinity Loop

� �$� 1 for ( ; ; ) ;

� �$� 2 while (true) ;

� �$� 3 do { } while (true);

Java Programming Language by Rungrote Phonkam

Page 45: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumps

������

break Label_Name

�����

Label_Name ���"������ ��$������+�&!��(����������������*#��� ���,��&�%������������ � ������&���� *���%�-��������*# ���,�� ���&��������������$�������% ���,��

������

break Label_Name

�����

Label_Name ���"������ ��$������+�&!��(����������������*#��� ���,��&�%������������ � ������&���� *���%�-��������*# ���,�� ���&��������������$�������% ���,��

Java Programming Language by Rungrote Phonkam

Page 46: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumpsclass BreakScope1{ public static void main(String args[])

{ for(int a=0; a<10; a++){ System.out.println(“Line 1”);

break;System.out.println(“Line 2”);

}System.out.println(“Line 3”);

}}

class BreakScope1{ public static void main(String args[])

{ for(int a=0; a<10; a++){ System.out.println(“Line 1”);

break;System.out.println(“Line 2”);

}System.out.println(“Line 3”);

}}

Java Programming Language by Rungrote Phonkam

Page 47: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumpsclass BreakScope2{ public static void main(String args[])

{ Comehere: for(int a=0; a<10; a++){ System.out.println(“Line 1”);

for(int a=0; a<10; a++){ System.out.println(“Line 2”);

break Comehere;}

}System.out.println(“Line 3”);

}}

class BreakScope2{ public static void main(String args[])

{ Comehere: for(int a=0; a<10; a++){ System.out.println(“Line 1”);

for(int a=0; a<10; a++){ System.out.println(“Line 2”);

break Comehere;}

}System.out.println(“Line 3”);

}}

Java Programming Language by Rungrote Phonkam

Page 48: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumps

������

continue label

�����

label ���"������ ��$������+�&!��(����������������*#��� ��,��&�%������������ � ������&���� *���%�-��������*# ���,�� ���&��������������$���.���� ���,�� ������ ������%����,���#$%!�� ������� continue ����

������

continue label

�����

label ���"������ ��$������+�&!��(����������������*#��� ��,��&�%������������ � ������&���� *���%�-��������*# ���,�� ���&��������������$���.���� ���,�� ������ ������%����,���#$%!�� ������� continue ����

Java Programming Language by Rungrote Phonkam

Page 49: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumpsclass OddNumber

{ public static void main(String args[]){ for (int a=1; a<=100; a+=1)

{ if ((a%2)==0)continue;

System.out.println(a + “ is Odd Number”);}

}}

class OddNumber{ public static void main(String args[])

{ for (int a=1; a<=100; a+=1){ if ((a%2)==0)

continue;System.out.println(a + “ is Odd Number”);

}}

}

Java Programming Language by Rungrote Phonkam

java OddNumber1 is Odd Number…99 is Odd Number

Page 50: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumpsclass BreakScope3{ public static void main(String args[])

{ Comehere: for(int a=0; a<10; a++){ System.out.println(“Line 1”);

for(int a=0; a<10; a++){ System.out.println(“Line 2”);

continue Comehere;}

}System.out.println(“Line 3”);

}}

class BreakScope3{ public static void main(String args[])

{ Comehere: for(int a=0; a<10; a++){ System.out.println(“Line 1”);

for(int a=0; a<10; a++){ System.out.println(“Line 2”);

continue Comehere;}

}System.out.println(“Line 3”);

}}

Java Programming Language by Rungrote Phonkam

Page 51: Java Programming Language Mr.Rungrote Phonkam …it.e-tech.ac.th/poohdevil/JavaConcepts/JavaConcepts05.pdf · System.out.println(“Hello Java”); 1 2 2. Control Flows ˘ ˇ (Conditions)

2.4. Jumps2.4. Jumps2.4. Jumps

������

return Expression

�����

Expression ���'������$������������� ���� ����������'����)��#

� �"������� ����%*#��������'����)��#

� �������)��#��� �����&#� �$%��!.��# return ���#�!%����$��������������

������

return Expression

�����

Expression ���'������$������������� ���� ����������'����)��#

� �"������� ����%*#��������'����)��#

� �������)��#��� �����&#� �$%��!.��# return ���#�!%����$��������������

Java Programming Language by Rungrote Phonkam