Control Structures Flow of Execution Selection b b

Control Structures ��������


Flow of Execution

Selection ประกอบดวย ลกษณะการทำงานดงน b b b One-Way Selection Two-Way Selection Compound (Block of) Statements Multiple Selections (Nested if( switch Structures

One-Way Selection b b b Syntax: if(expression) statement Expression ����������� Statement ������������

The if Statement b The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped. 6

The If Statement and If – else Statement b Form of the if Statement • if ( <conditional expression> ) <statement> • if ( < conditional expression > ) <statement 1> else <statement 2> – Result of conditional expression : Logical Type(true or false)

The If and If-Else Statement b Nested if statement if (<cond. expr. >) //. . . <statement> if (<cond. expr. 1>) <statement 1> else if (<cond. expr. 2>) < statement 2> … else if (<cond. expr. n>) < statement n> else < statement> n A Block of if (expression) { Code statement 1; statement 2; …………. }


The if Statement b An example of an if statement: if (sum > MAX) delta = sum - MAX; System. out. println ("The sum is " + sum); First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not. If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.

import javax. swing. JOption. Pane; class If 1 { public static void main (String args[]) { int score = 50; if (score > 0) { String message = "score = " + score + "grade = pass" ; JOption. Pane. show. Message. Dialog(null, message); System. exit(0); } } }

Two-Way Selection b b Syntax: if(expression) statement 1 else statement 2 else ��������� 1 ������� statement ��� 2 ���

Two-Way Selection

Example if (hours > 40. 0) wages = 40. 0 * rate + 1. 5 * rate *(hour-40. 0); else wage = hours * rate;

Logic of an if statement condition evaluated true statement false

The if-else Statement b An else clause can be added to an if statement to make it an if-else statement: if ( condition ) statement 1; else statement 2; b If the condition is true, statement 1 is executed; if the condition is false, statement 2 is executed b One or the other will be executed, but not both 16

Logic of an if-else statement condition evaluated true false statement 1 statement 2

import javax. swing. JOption. Pane; class If 2 { public static void main (String args[]) { double score ; String message, data; data = JOption. Pane. show. Input. Dialog(null, "Enter score : "); score = Double. parse. Double(data); if (score >= 49) message = "score = " + score + "grade = Pass"; else message = "score = " + score + "grade = F" ; JOption. Pane. show. Message. Dialog(null, message); System. exit(0); } }

Compound (Block of) Statements b } �������� (statement) ���� if ����� 1 ����������� if(age >18) ������ { } �������� Syntax { statement 1 statement 2. . . statementn { System. out. println(“Eligible to vote. ”); System. out. println(“No longer a minor. ”); } else { System. out. println(“Not eligible to vote. ”); System. out. println(“still a minor. ”); }

Block Statements b Several statements can be grouped together into a block statement b A block is delimited by braces ( { … } ) b A block statement can be used wherever a statement is called for in the Java syntax b For example, in an if-else statement, the if portion, or the else portion, or both, could be block statements 20

Multiple Selection: Nested if b Syntax if(expression 1) statement 1 else if(expression 2) statement 2 else statement 3 b b b ���������� if �������� ������� else if �������� else ��������

Example if(balance > 50000. 00) intererest. Rate = 0. 07; else if (balance >=25000. 00) intererest. Rate = 0. 05; else if (balance >=10000. 00) intererest. Rate = 0. 03; else intererest. Rate = 0. 00;

import javax. swing. JOption. Pane; class If 3 { public static void main (String args[]) { double score ; String message, data, grade; data = JOption. Pane. show. Input. Dialog(null, "Enter score : "); score = Double. parse. Double(data); if (score <= 49) { grade = "E"; } else if ( score <= 59 ) { grade =" D"; }

else if ( score <= 69) {grade =" C"; } else if ( score <= 79) {grade =" B"; } else {grade =" A"; } message = " score = " + score + "grade = "+grade; JOption. Pane. show. Message. Dialog(null, messa ge); System. exit(0); } }


The switch Statement b The general syntax of a switch statement is: switch and case are reserved words switch ( expression ) { case value 1 : statement-list 1 case value 2 : statement-list 2 case value 3 : statement-list 3 case. . . } If expression matches value 2, control jumps to here

switch Structures switch(expression) { case value 1: statements 1 break; case value 2: statements 2 break; . . . case valuen: statementsn break; default: statements } b Expression ����������������� �

switch Statement

Example I switch(grade) { case ‘A’: System. out. println(“grade is A”); break; case ‘B’: System. out. println(“grade is B”); break; case ‘C’: System. out. println(“grade is C”); break; case ‘D’: System. out. println(“grade is D”); break; case ‘F’: System. out. println(“grade is F”); break; default: System. out. println(“grade is invalid”); }


Example II switch(score/10) { case 0: case 1: case 2: case 3: case 4: case 5: grade = ‘F’; break; case 6: grade=‘D’; break; case 7: grade=‘C’; break; case 8: grade=‘B’; break; case 9: case 10: grade=‘A’; break; default: System. out. println(“Invalid test score”); }
![public class Switch. Demo } public static void main(String[] args} ( int month = public class Switch. Demo } public static void main(String[] args} ( int month =](http://slidetodoc.com/presentation_image_h/2777d70c5d9dedc96457c3cd6369caac/image-32.jpg)
public class Switch. Demo } public static void main(String[] args} ( int month = 8; switch (month} ( case 1: System. out. println("January"); break; case 2: System. out. println("February"); break; case 3: System. out. println("March"); break; case 4: System. out. println("April"); break; case 5: System. out. println("May"); break; case 6: System. out. println("June"); break; case 7: System. out. println("July"); break; case 8: System. out. println("August"); break; case 9: System. out. println("September"); break; case 10: System. out. println("October"); break; case 11: System. out. println("November"); break; case 12: System. out. println("December"); break; { {



if (7 < 8) { System. out. println(“ 2 4 6 8”); System. out. println(“ 1 3 5 7”); } ������� …………………………. if (5 < 3) System. out. println(“*”); else if (7 == 8) System. out. println(“&”); else System. out. println(“$”); ������� ………………………….

x = 100; y = 200; if (x > 100 && y <= 200) System. out. println(x + y); else System. out. println(2 * x – y); ������� …………………………. alpha = 5; switch (alpha) { case 1 : case 2 : alpha = alpha + 2; break; case 4 : alpha++; case 5 : alpha = 2 * alpha; case 6 : alpha = alpha + 5; break; default : alpha- -; } ������� alpha = …………………….

beta = 3; switch (beta) { case 3 : beta = beta + 3; case 1 : beta++; break; case 5 : beta = beta + 5; case 4 : beta = beta + 4; } ������� beta = ……………………. a = 6; if (a > 0) switch (a) { case 1 : a = a + 3; case 3 : a++; break; case 6 : a = a + 6; case 8 : a = a * 8; break; default : a- -; } else a = a + 2; ������� a = ……………….


Exercise b ให �������������� 5 ������� ) Find. Total_Avg. java) Enter number 1 : Enter number 2 : Enter number 3 : Enter number 4 : Enter number 5 : Sum = Average =







The End
- Slides: 46