control statement

Page 1

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

ďœŠ Comparison Operators 4/56 Arithmetic Operators Logical NOT (!) &'(*) +,-.*/0)*1*20' 3 ďœŠ Logical AND (&, &&) :(; Logical OR (|, ||) :(; EXCLUSIVE OR (^) 1 2 4 3 if (total/count > MAX && count != 0) System.out.println ("Testing‌"); 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


LS2./;-N2.*/4*U((,MVX61 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((,MVX61 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 LS2#$% expression 2 b;\^.]/Nbc6-LM536 4*U((,MVX61 Boolean expression 2'้ - 4*. expression 2 &'a*LS2#$% Boolean expression 2'้b;&' a*LS2#$% - 4*. expression 2 &'a*LS2&'(# Boolean expression 2'้b;&' a*LS2&'(# \* expression 1 LS2&'(# expression 2 b;d&\^.]/Nbc6-6'. h+[U((,MVX61 Boolean expression 2'้b;&'a*LS2&'(#

10


S/;h[a0'3.)*42+.*/0)*1*2X61S/;h[a6532i j2hS/:./&N*b; j40)*1*24/56d&j40)*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 LS2.*/L/n3&]2 if statement

]61LS2 boolean expression 0'3&'a*LS2 true 4/56 false

if (expression expression)) { statement; } \* expression LS2b/n1 \* expression LS2L0qb

: statement b;\^. Executed : statement b;d&\^. Executed

12


1. 2. 3.

L156 3 2dX score b;\^.]/Nbc6\*L15362dXLS2b/n1 (score &'a*],้1:] 50 Xr้2dS) “Pass” b;\^.Mn&M66.0*1 42*b6 \*L15362dXLS2L0qb (score &'a*26[.N* 50) d&&'.*/+)*L2n2.*/j+i j2hS/:./& 13


The if-else statement &' Syntax +,12'้

if (expression expression)) { statement 1; } else { statement 2; } \* expression LS2b/n1 : statement 1 b;\^. Executed \* expression LS2L0qb : statement 2 b;\^. Executed 14


L156 3 2dX score b;\^.]/Nbc6\*L15362dXLS2b/n1 (score &'a*],้1:] 50 Xr้2dS) “Pass” b;\^.Mn&M66.0*142*b6 \*L15362dXLS2L0qb (score &'a*26[.N* 50) “Fail” b;\^.Mn&M66.0*142*b6 15


The if `ďœ‹62 if statement &' Syntax +,12'ŕš‰ if (expression 1) { if (expression 2) { statement 2; } else if (expression 3){ statement 3; } else { statement else; } } else { }

statement 1 23 $45'5 &6ďœ’3&'(#; 16


The if `ďœ‹62 if statement &' Syntax +,12'ŕš‰ if (expression 1) { statement 1 23 $45'5 &6ďœ’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'ŕš‰ 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=“ � num T

F

num == 0 T

result=“ZERO�

num > 0

result=“PLUS�

F

result=“MINUS�

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 – 25

Normal

25 – 30

Over weight

>= 30

Obese

27


Problem 1 : BMI (Body Mass Index)

],N6[ďœŠ*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&ďœŠ&'.qd+ďœ‹

29


expression j2 switch statement ]61&' 2n+X6&^( (Data type) LS2 int 4/56 char L0*2,้2 break statement 6*b\^.j LS2 statement c +0*[j2:](; case statement break statement b;aN-a &.*/0)*1*2X61hS/:./&j466.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])*:421j+.qd+j2 switch :]h+[0,3NdS b;LX'[26[^0'3])*:4210*[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; j40)*1*24/56d&j40)*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'ŕš‰

for WSQ reserve word &6ďœ’3 $&$% D ďœ‹3 for statement

* N3FWďœŠ &$% D ďœ‹3 YZ

#[' 3 &_5O W$GH &F5O ďœŠQ3&$% D loop

' $ $ # QR& S Q3TU #3 ďœŠ ]^^G_`ďœŽ#[&6ďœ’3&'(#

for (initialization (initialization;; condition condition;; update update)) { statement; &_% DWďœŠ / ^FWďœŠ #[' 323 ^ FGR PF'ďœ‹ OUQ b ďœŠ^[$QR } $' 3

' 3 ( ďœŠQ&DS Q& S Q3TU&6ďœ’3#$%

34


Example

// 6$[ cWďœŠ W '5

aďœŠ*L/n3&]ďœ‹2

L15362dX

LMn3&aďœŠ*/(+aďœŠ*

Initialize

##3D4 1 2 3 End‌

Condition True Statement

Iteration

False

Update 35


Example ]61.*/Mn&ML(Xa'3b*. 1 \r1 10 `r31:c+1-242*b6LS2 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/:./&/,-X6&^(],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=“ �, n

n <= 15

F

end

T num

15 /6T result=“ZERO�

num == 0 T result=“PLUS�

F num > 0

F

result=“MINUS�

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=“ �, 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=“ZERO� cz++

end

num == 0 T result=“PLUS� cp++

F num > 0

F

result=“MINUS� 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'ŕš‰ while WSQ reserve word &6ďœ’3 $&$% D ďœ‹3 while statement

' $ $ # QR& S Q3TU #3 ďœŠ ]^^G_`ďœŽ#[&6ďœ’3&'(#

while (expression expression)) { statement statement;; }

\ďœ‹* expression LSďœ’2b/n1 statement b;\^. Executed b*.2,ŕš‰2 expression b;\^.]/Nbc6-6'.j2/6-\,+dS statement b;\^.0)*1*2`ŕš‰)*b2.NďœŠ* expression b;LSďœ’2L0qb

41


4/56 The while statement &' Syntax +,12'ŕš‰ initialization; while (expression expression)) { statement; update; }

N3FWďœŠ &$%D

ďœ‹3 ' $ $ # QR& S Q3TU #3 ďœŠ ]^^G_`ďœŽ#[&6ďœ’3&'(# &_S DWďœŠ / ^FWďœŠ

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'ŕš‰ do, while WSQ reserve word &6ďœ’3 $&$% D ďœ‹3 do statement

Statement #[g Execute ďœŠQ3& DQ # 3GH3#Z ' $ $ # QR expression

do { statement statement;; } while (expression expression); ); ' $ $ # QR& S Q3TU #3 ďœŠ ]^^G_`ďœŽ#[&6ďœ’3&'(#

Statement b;\^. Executed j2/6-:/.Lc&6 b*.2,ŕš‰2 expression b;\^.]/Nbc6-\ďœ‹* expression LSďœ’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 – 25

Normal

25 – 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 – 25

Normal

25 – 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\*jcL(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 ‌ ============================= Count Grade A = ??? Count Grade B = ???

Count Grade C = ??? Count Grade D = ??? Count Grade F = ???

56


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.