Comparison Operators Boolean Logical Operators - Conditional Statements - Repetition Statement -
-
Watcharin Puangplia : 20/ 20/12/ 12/53 1
Operator < <= > >= == !=
Name less than less than or equal to greater than greater than or equal to equal to not equal to
2
Operator ! & && | || ^
Name not and and (Short-circuit) or or (Short-circuit) exclusive or
3
Comparison Operators (<, <=, >, >=, ==, !=) Arithmetic Operators (+, -, *, /, %) Boolean Logical Operators (!, &, &&, |, ||, ^) Boolean Logical Operators &'()*+,-.*/0)*1*20'
3
ď&#x153;&#x160; Comparison Operators 4/56 Arithmetic Operators Logical NOT (!) &'(*) +,-.*/0)*1*20' 3 ď&#x153;&#x160; Logical AND (&, &&) :(; Logical OR (|, ||) :(; EXCLUSIVE OR (^) 1 2 4 3 if (total/count > MAX && count != 0) System.out.println ("Testingâ&#x20AC;Ś"); 4
p
!p
T
F
F
T Example
!(1>0) = F
LM/*;N* (1>0) = T
!(1>2) = T
LM/*;N* (1>2) = F
5
p
q
p && q
T
T
T
T
F
F
F
T
F
F
F
F
Example (3>2) && (5>=5) = T
LM/*;N* (3>2) = T, (5>=5) = T
(3<2) && (5<=5) = F
LM/*;N* (3<2) = F, (5<=5) = T 6
p
q
p || q
T
T
T
T
F
T
F
T
T
F
F
F
Example (3>2) || (5>5) = T
LM/*;N* (3>2) = T, (5>5) = F
(3==2) || (5<5) = F
LM/*;N* (3==2) = F, (5<5) = F 7
p
q
p^q
T
T
F
T
F
T
F
T
T
F
F
F
Example (3>2) ^ (5>5) = T
LM/*;N* (3>2) = T, (5>5) = F
(3==2) ^ (5<5) = F
LM/*;N* (3==2) = F, (5<5) = F 8
LS2./;-N2.*/4*U((,MVX61 Boolean expression +N[ Logical Operators AND 4/56 OR 6[*1/N-/,+4/566[*1[6 \*],N\^../;0)* (Operand) 0'36[^0*1`*[&56X61 Logical Operator &'a*LM'[1M60'3b;c*&*/\.)*42+U((,MVX61 expression d+ ],N\^../;0)*0'36[^0*1XN*&56X61 Logical Operator b;d&\^.2)*&*]/Nbc6-6'.
9
if (expression 1 && expression 2) System.out.println(“Testing…”);
\* expression 1 LS2#$% expression 2 b;\^.]/Nbc6-LM536 4*U((,MVX61 Boolean expression 2'้ - 4*. expression 2 &'a*LS2#$% Boolean expression 2'้b;&' a*LS2#$% - 4*. expression 2 &'a*LS2&'(# Boolean expression 2'้b;&' a*LS2&'(# \* expression 1 LS2&'(# expression 2 b;d&\^.]/Nbc6-6'. h+[U((,MVX61 Boolean expression 2'้b;&'a*LS2&'(#
10
S/;h[a0'3.)*42+.*/0)*1*2X61S/;h[a6532i j2hS/:./&N*b; j40)*1*24/56d&j40)*1*2j2()*+,-\,+dS S/;h[aL15362dXj2 Java &'+,12'้ - The if statement - The if-else statement - The if `62 if statement - The if 4(*[0*1L(56. statement - The switch statement
11
The if statement &' Syntax +,12'้
if a56 reserve word LS2.*/L/n3&]2 if statement
]61LS2 boolean expression 0'3&'a*LS2 true 4/56 false
if (expression expression)) { statement; } \* expression LS2b/n1 \* expression LS2L0qb
: statement b;\^. Executed : statement b;d&\^. Executed
12
1. 2. 3.
L156 3 2dX score b;\^.]/Nbc6\*L15362dXLS2b/n1 (score &'a*],้1:] 50 Xr้2dS) “Pass” b;\^.Mn&M66.0*1 42*b6 \*L15362dXLS2L0qb (score &'a*26[.N* 50) d&&'.*/+)*L2n2.*/j+i j2hS/:./& 13
The if-else statement &' Syntax +,12'้
if (expression expression)) { statement 1; } else { statement 2; } \* expression LS2b/n1 : statement 1 b;\^. Executed \* expression LS2L0qb : statement 2 b;\^. Executed 14
L156 3 2dX score b;\^.]/Nbc6\*L15362dXLS2b/n1 (score &'a*],้1:] 50 Xr้2dS) “Pass” b;\^.Mn&M66.0*142*b6 \*L15362dXLS2L0qb (score &'a*26[.N* 50) “Fail” b;\^.Mn&M66.0*142*b6 15
The if `ď&#x153;&#x2039;62 if statement &' Syntax +,12'ŕš&#x2030; if (expression 1) { if (expression 2) { statement 2; } else if (expression 3){ statement 3; } else { statement else; } } else { }
statement 1 23 $45'5 &6ď&#x153;&#x2019;3&'(#; 16
The if `ď&#x153;&#x2039;62 if statement &' Syntax +,12'ŕš&#x2030; if (expression 1) { statement 1 23 $45'5 &6ď&#x153;&#x2019;3#$% ; } else { if (expression 2) { statement 2; } else if (expression 3){ statement 3; } else { statement 4; } } // else
17
! " #$ % & ! ! $ 15000 15,000 70,000 70,000 100,000 100,000
'
Platinum
( ( ) )* + ,-* ,. / + - ) ,-* 01 +2)* )* + 345 +26 7 8 + 9 15,000 ): )*
18
19
The if 4(*[0*1L(56. Syntax +,12'ŕš&#x2030; if (expression 1) { statement 1; } else if (expression 2) { statement 2; } else if (expression 3) { statement 3; } else { statement else; }
if (expression 1) statement 1; else if (expression 2) statement 2; else if (expression 3) statement 3; else statement else;
20
Ex.Grade
&% "2
"! & 3!" 4 % " 1 & 264
&$ & 7 80 :7 70 : 79 60 : 69 50 : 59 " 50 if (score >= 80) System.out.println("A"); else if (score >= 70) System.out.println("B"); else if (score >= 60) System.out.println("C"); else if (score >= 50) System.out.println("D"); else System.out.println("F");
A B C D F if (score >= 80) { System.out.println("A"); } else if (score >= 70) { System.out.println("B"); } else if (score >= 60) { System.out.println("C"); } else if (score >= 50) { System.out.println("D"); } else { System.out.println("F"); }
21
##3D4 Input Score = 73 Grade : B
22
, result >:? .92 @ 0-- .65A , 0- 3 ##3D4 Input Score = 73 Grade : B
23
##3D4 Input Score = 104 Grade : -- Error --
24
1. ) >+ / , > 1- ->A C 7 :2 ) 7 -> +2 > 92 . ) 3 , * D ,- ->E1 5 ->) 7 9 ->- start Enter number = 9 Result = PLUS num, result=â&#x20AC;&#x153; â&#x20AC;? num T
F
num == 0 T
result=â&#x20AC;&#x153;ZEROâ&#x20AC;?
num > 0
result=â&#x20AC;&#x153;PLUSâ&#x20AC;?
F
result=â&#x20AC;&#x153;MINUSâ&#x20AC;?
result end 25
2. ) >+ / , , 6+ * +2, > K> 01 +? K 10 L * ? -* 2 8 8 start K 3 L : 10 L * ? -* 1 8 8 K 1 L : 3 L * ? -* 1/2 8 8 age, eat , : 1 L 7 * Age
Enter Age Eat Drug
= =
0.5 0 spoon
T eat = 2
age > 10
F
T
age > 3
eat = 1
T eat = 0.5
F age > 1
F eat = 0
Eat end
26
Problem 1 : BMI (Body Mass Index) 3. / , 3 8 + - ) ? 7 ( .) ,-* 1 ( .) Q:2 + -* + +?
BMI = weight/height2
BMI
Weight Status
< 18.5
Under weight
18.5 â&#x20AC;&#x201C; 25
Normal
25 â&#x20AC;&#x201C; 30
Over weight
>= 30
Obese
27
Problem 1 : BMI (Body Mass Index)
],N6[ď&#x153;&#x160;*1 Weight is Height is BMI (58 / 1.65 * 1.65)
: 58 : 1.65 : 21.3
kg./m.
- - - - - - - - - - - - - - - BMI - - -- - - - - - - - - - - - - - - Weight Status : Normal - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - -
28
The switch statement D5 Syntax FG 35H switch (expression) { case value 1: statement 1; [break;] case value 2: statement 2; [break;] case value n: statement n; [break;] [break : statement; break;] }
ND O&N P a)*c,31j2La/53614&*[ [] b;&'4/56d&ď&#x153;&#x160;&'.qd+ď&#x153;&#x2039;
29
expression j2 switch statement ]61&' 2n+X6&^( (Data type) LS2 int 4/56 char L0*2,้2 break statement 6*b\^.j LS2 statement c +0*[j2:](; case statement break statement b;aN-a &.*/0)*1*2X61hS/:./&j466.b*. switch statement L&5360)*1*2j2L15362dX0'3]/1.,- expression :(N 4*.d&j break statement .*/0)*1*2X61hS/:./&b;0)*1*2 j2L15362dX0'3]/1.,- expression b*.2,้2b;0)*1*2]6j2 case statement list \+dSb2a/-
30
switch statement 6*b&' default case d+ default case 6*b6[^0'3])*:421j+.qd+j2 switch :]h+[0,3NdS b;LX'[26[^0'3])*:4210*[c +X61 switch statement j2 default case b;0)*1*2.q]6L&536d&&'a*X61 case j+0'3]/1 (Match) .,- expression j2 switch statement \*j2 switch statement d&&' default case :(;d&&'a*X61 case j+0'3]/1 (Match) .,- expression j2 switch statement hS/:./&b;66.b*. switch statement :(;0)*1*2]6j2 statement 6532i 4(,1b*. switch statement
31
Example
##3D4 Input Choise = 3 Choise 3
32
S/;h[a0'3.)*42+.*/0)*1*2X61S/;h[a6532i j2hS/:./&N*b; j40)*1*24/56d&j40)*1*2j2(,. ;.*/0)*`้)* (loop) S/;h[a0)*`้)*j2 Java &'+,12'้ - The for loop - The while loop - The do loop
33
The for statement &' Syntax +,12'ŕš&#x2030;
for WSQ reserve word &6ď&#x153;&#x2019;3 $&$% D ď&#x153;&#x2039;3 for statement
* N3FWď&#x153;&#x160; &$% D ď&#x153;&#x2039;3 YZ
#[' 3 &_5O W$GH &F5O ď&#x153;&#x160;Q3&$% D loop
' $ $ # QR& S Q3TU #3 ď&#x153;&#x160; ]^^G_`ď&#x153;&#x17D;#[&6ď&#x153;&#x2019;3&'(#
for (initialization (initialization;; condition condition;; update update)) { statement; &_% DWď&#x153;&#x160; / ^FWď&#x153;&#x160; #[' 323 ^ FGR PF'ď&#x153;&#x2039; OUQ b ď&#x153;&#x160;^[$QR } $' 3
' 3 ( ď&#x153;&#x160;Q&DS Q& S Q3TU&6ď&#x153;&#x2019;3#$%
34
Example
// 6$[ cWď&#x153;&#x160; W '5
aď&#x153;&#x160;*L/n3&]ď&#x153;&#x2039;2
L15362dX
LMn3&aď&#x153;&#x160;*/(+aď&#x153;&#x160;*
Initialize
##3D4 1 2 3 Endâ&#x20AC;Ś
Condition True Statement
Iteration
False
Update 35
Example ]61.*/Mn&ML(Xa'3b*. 1 \r1 10 `r31:c+1-242*b6LS2 1 3 5 7 9
##3D4 Print odd value from 1 to 10 by for-statement 1 3 5 7 9
36
Example b1LX'[2hS/:./&/,-X6&^(],NL(Xj+ i 5 a*
##3D4 Enter Number = 4 Enter Number = 7.45 Enter Number = 2.28 Enter Number = 9 Enter Number = 14 37
4. ) >+ / , > 1- ->A C 15 ) 7 -> +2 > 92 . ) 3 , * D start ,- ->E1 5 ->) 7 9 ->- Enter number = 9 Result = PLUS Enter number = 0 Result = ZERO .. .
Enter number = -4 Result = MINUS
num, result=â&#x20AC;&#x153; â&#x20AC;?, n
n <= 15
F
end
T num
15 /6T result=â&#x20AC;&#x153;ZEROâ&#x20AC;?
num == 0 T result=â&#x20AC;&#x153;PLUSâ&#x20AC;?
F num > 0
F
result=â&#x20AC;&#x153;MINUSâ&#x20AC;?
result n++ 38
4.1. ) >+ / , > 1- ->A C 15 ) 7 -> +2 > 92 . ) 3 , * D start ,- ->E1 5 ->) 7 9 ->- Enter number = 9 Result = PLUS Enter number = 0 Result = ZERO .. .
num, result=â&#x20AC;&#x153; â&#x20AC;?, n, cz=0, cp=0, cm=0
n <= 15
cz,cp,cm
T num
15 /6-
Enter number = -4 Result = MINUS ===================== Count Plus = 5 Count Minus = 2 Count Zero = 3
F
T result=â&#x20AC;&#x153;ZEROâ&#x20AC;? cz++
end
num == 0 T result=â&#x20AC;&#x153;PLUSâ&#x20AC;? cp++
F num > 0
F
result=â&#x20AC;&#x153;MINUSâ&#x20AC;? cm++
result n++ 39
5. ) >+ / , , 6+ * > 01 W ) 20 ,-* ) 01 W A , -* 8 K +2, > K> 01 +? start K 10 L * ? -* 2 8 8 K 3 L : 10 L * ? -* 1 8 8 age, eat K 1 L : 3 L * ? -* 1/2 8 8 Age , : 1 L 7 * T
Enter Age = Eat Drug = Enter Age = Eat Drug =
0.5 1 spoon 23 2 spoon
eat = 2
age > 10
F
T
age > 3
eat = 1
T eat = 0.5
Count Count Count Count
age age age age
> > > <
10 = ??? people 3 = ??? people 1 = ??? people 1 = ??? people
F age > 1
F eat = 0
Eat end
40
The while statement &' Syntax +,12'ŕš&#x2030; while WSQ reserve word &6ď&#x153;&#x2019;3 $&$% D ď&#x153;&#x2039;3 while statement
' $ $ # QR& S Q3TU #3 ď&#x153;&#x160; ]^^G_`ď&#x153;&#x17D;#[&6ď&#x153;&#x2019;3&'(#
while (expression expression)) { statement statement;; }
\ď&#x153;&#x2039;* expression LSď&#x153;&#x2019;2b/n1 statement b;\^. Executed b*.2,ŕš&#x2030;2 expression b;\^.]/Nbc6-6'.j2/6-\,+dS statement b;\^.0)*1*2`ŕš&#x2030;)*b2.Nď&#x153;&#x160;* expression b;LSď&#x153;&#x2019;2L0qb
41
4/56 The while statement &' Syntax +,12'ŕš&#x2030; initialization; while (expression expression)) { statement; update; }
N3FWď&#x153;&#x160; &$%D
ď&#x153;&#x2039;3 ' $ $ # QR& S Q3TU #3 ď&#x153;&#x160; ]^^G_`ď&#x153;&#x17D;#[&6ď&#x153;&#x2019;3&'(# &_S DWď&#x153;&#x160; / ^FWď&#x153;&#x160;
42
##3D4 0 1 Done!
Condition True
Iteration
False
Statement
43
Condition
##3D4 0 1 Done!
True
Iteration
False
Statement 44
// & !
// $ "
// 3! & ( $ # & 45
// & !
// $ "
// 3! & ( $ # &
46
The do statement &' Syntax +,12'ŕš&#x2030; do, while WSQ reserve word &6ď&#x153;&#x2019;3 $&$% D ď&#x153;&#x2039;3 do statement
Statement #[g Execute ď&#x153;&#x160;Q3& DQ # 3GH3#Z ' $ $ # QR expression
do { statement statement;; } while (expression expression); ); ' $ $ # QR& S Q3TU #3 ď&#x153;&#x160; ]^^G_`ď&#x153;&#x17D;#[&6ď&#x153;&#x2019;3&'(#
Statement b;\^. Executed j2/6-:/.Lc&6 b*.2,ŕš&#x2030;2 expression b;\^.]/Nbc6-\ď&#x153;&#x2039;* expression LSď&#x153;&#x2019;2b/n1 statement br1b;\^. Execute 6'.j2/6-\,+dS 47
##3D4 0 1 Done!
Condition
Iteration
Statement
True
False 48
6. ) >+ / , > 1- ->A C 15 ) 7 -> +2 > 92 . ) 3 , * D ,- ->E1 5 ->) 7 9 ->- Enter number = 9 Result = PLUS Enter number = 0 Result = ZERO
15 /6-
.. .
Enter number = -4 Result = MINUS
49
6.1. ) >+ / , > 1- ->A C 15 ) 7 -> +2 > 92 . ) 3 , * D ,- ->E1 5 ->) 7 9 ->- Enter number = 9 Result = PLUS Enter number = 0 Result = ZERO .. .
15 /6-
Enter number = -4 Result = MINUS ===================== Count Plus = 5 Count Minus = 2 Count Zero = 3 50
7. ) >+ / , , 6+ * > 01 W ) 20 ,-* ) 01 W A , -* 8 K +2, > K> 01 +? K 10 L * ? -* 2 8 8 K 3 L : 10 L * ? -* 1 8 8 K 1 L : 3 L * ? -* 1/2 8 8 , : 1 L 7 * Enter Age = Eat Drug = Enter Age = Eat Drug = Count Count Count Count
age age age age
> > > <
0.5 1 spoon 23 2 spoon
10 = ??? people 3 = ??? people 1 = ??? people 1 = ??? people 51
Problem 1_1 : BMI (Body Mass Index) 8. / , 3 8 + - ) ? 7 ( .) ,-* 1 ( .) > 01 W ) 50 . ) *A , -* * +01 W ) A -* + +?
BMI = weight/height2
BMI
Weight Status
< 18.5
Under weight
18.5 â&#x20AC;&#x201C; 25
Normal
25 â&#x20AC;&#x201C; 30
Over weight
>= 30
Obese
52
Problem 1_1 : BMI (Body Mass Index)
],N6[*1 Weight is : 58 Height is : 1.65 BMI (58 / 1.65 * 1.65) : 21.3 kg./m. 50 /6-
- - - - - - - - - - - - - BMI - - -- - - - - - - - - - Weight Status : Normal - - - - - - - - - - - - - - - - - - -- - - - - - - - - - :. -
Count Under weight = ???? People Count Normal = ???? People Count Over weight = ???? People Count Obese = ???? People
c/ SU( .*/2,b)*2N2 53
Problem 1_2 : BMI (Body Mass Index) 8. 1./ , 3 8 + - ) ? 7 ( .) ,-* 1 ( .) > 01 W ) n . ) *A , -* * +01 W ) A -* + +?
BMI = weight/height2
BMI
Weight Status
< 18.5
Under weight
18.5 â&#x20AC;&#x201C; 25
Normal
25 â&#x20AC;&#x201C; 30
Over weight
>= 30
Obese
54
Problem 1_2 : BMI (Body Mass Index)
],N6[*1 Please select choise(1 = continue) : 1 Weight is : 58 Height is : 1.65 BMI (58 / 1.65 * 1.65) : 21.3 kg./m. - - - - - - - - - - - - - BMI - - -- - - - - - - - - - Weight Status : Normal - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -
b)*2N2 n /6\*jcL(X 1 0)*]6dS 26.b*.2,้2d&0)*
Please select choise(1 = continue) : 0 -
.. .
Count Under weight = ???? People Count Normal = ???? People Count Over weight = ???? People Count Obese = ???? People
c/ SU( .*/2,b)*2N2 55
Problem 2 : Grade evaluation 9. ) >+ / , 3 8 . 5> .E. 8 ? L +2 1 ) 30 345 +2A8 A 30 /) 5 Ex.Grade
Student Id : 001 Student Name : Ant Bird Input score1(20%) : 15.5 Input score2(40%) : 27 Input score3(40%) : 31 Total(15.5 + 27 + 31) = 73.5
point
Grade is B â&#x20AC;Ś ============================= Count Grade A = ??? Count Grade B = ???
Count Grade C = ??? Count Grade D = ??? Count Grade F = ???
56