Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Statements August 22, 2019 ICS102: The course.

Similar presentations


Presentation on theme: "Selection Statements August 22, 2019 ICS102: The course."— Presentation transcript:

1 Selection Statements August 22, 2019 ICS102: The course

2 Outline Block statement Branching Statements Nested if statements
Simple if statement if-else statement if-else-elseif statement switch statement Nested if statements August 22, 2019 ICS102: The course

3 - Block Statement A block statement consists of one or more Java statements enclosed in braces. Example of a block statement: { statement 1; statement 2; statement n; } Blocks can be nested. A block statement can be used anywhere that a single statement can be used. August 22, 2019 ICS102: The course

4 - Branching Statement A branching statement consists of one or more block statements The execution of a block statement in a branching statement is controlled by a boolean expression which we call a condition. There are manly the following four types of branching statement. Simple if If-else If-elseif-else switch August 22, 2019 ICS102: The course

5 -- Simple if Statement …
Simple if statement has the following structure: if ( <boolean_expression> ) { <then_block> } The boolean_expression must be enclosed in parentheses If the boolean_expression is true, then the then_block is executed. Otherwise it will NOT execute. if ( testScore >= ) { System.out.println("You are a good student"); } Then Block Boolean Expression August 22, 2019 ICS102: The course

6 -- Simple if Statement …
Chapter 6 -- Simple if Statement … testScore >= 95? false System.out.println ("You are a good student"); true Control Flow of if: August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

7 … -- Simple if Statement
Example: Design and write a Java program prints the absolute value of a number. import java.util.Scanner; class absolute { public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(“Enter a number: “); double x = kb.nextDouble(); double y = x; if( y < 0) { y = -y; } System.out.print(“The absolute value of “ + x + “ is “ + y); August 22, 2019 ICS102: The course

8 -- if-else Statement … An if-else statement chooses between two alternative statements based on the value of a Boolean expression If the boolean_expression is true, then the then_block is executed, otherwise the else_block is executed. if (<boolean_expression>) { <then_block> } else { <else_block> } if (testScore < 50) { System.out.println("You did not pass"); } else { System.out.println("You did pass"); } Then Block Else Block Boolean Expression August 22, 2019 ICS102: The course

9 -- if-else Statement … true false System.out.println ("You did pass");
Chapter 6 -- if-else Statement … System.out.println ("You did pass"); false testScore < 50 ? ("You did not pass"); true August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

10 Chapter 6 Compound Statements You have to use braces if the <then> or <else> block has multiple statements. if only one statement is there braces are optional but it is advisable to always use them to enhance readability if (testScore < 70) { System.out.println("You did not pass"); System.out.println("Try harder next time"); } else System.out.println("You did pass"); System.out.println("Keep up the good work"); Then Block Else Block Rules for writing the then and else blocks: - Left and right braces are necessary to surround the statements if the then or else block contains multiple statements. - Braces are not necessary if the then or else block contains only one statement. - A semicolon is not necessary after a right brace. August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

11 … -- if-else Statement Design and write a Java program which prints the difference of two numbers. Scanner kb = new Scanner(System.in); System.out.println(“Enter the value of the first number: “); double first = kb.nextDouble(); System.out.println(“Enter the value of the second number: “); double second = kb.nextDouble(); if ( first > second) { double diff = first – second; System.out.println(diff); } else double diff = second – first; August 22, 2019 ICS102: The course

12 - Nested if Statements …
One of the block statements of a branching statement can be another if statement. The inner if statement is executed when the enclosing block statement is executed If statements can be nested to many levels If(<boolean_expresion_1>) { Statement_1; <block_statement_2>; Statement_3; } if(<boolean_expression_2>) Statement_2A; Statement_2B:  Statement_2A and 2B are only executed if boolean_expresions 1 and 2 are true. August 22, 2019 ICS102: The course

13 - Nested if Statements …
Chapter 6 - Nested if Statements … if (testScore >= 50) { if (studentAge < 10) { System.out.println("You did a great job"); } else { System.out.println("You did pass"); } } else { //test score < 50 System.out.println("You did not pass"); Nested if It is possible to write if tests in different ways to achieve the same result. For example, the above code can also be expressed as if (testScore >= 70 && studentAge < 10) { messageBox.show("You did a great job"); } else { //either testScore < 70 OR studentAge >= 10 if (testScore >= 70) { messageBox.show("You did pass"); messageBox.show("You did not pass"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

14 - Nested if Statements …
Chapter 6 - Nested if Statements … System.out.println ("You did not pass"); false inner if ("You did pass"); testScore >= 50 ? true studentAge < 10 ? ("You did a great job"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

15 -- if-elseif-else Statement …
The multiway if-else statement is simply a normal if-else statement that nests another if-else statement at every else branch It is indented differently from other nested statements All of the Boolean_Expressions are aligned with one another, and their corresponding actions are also aligned with one another The Boolean_Expressions are evaluated in order until one that evaluates to true is found The final else is optional August 22, 2019 ICS102: The course

16 if - else- if Test Score Grade A B C D N 85  score 75  score  85
Chapter 6 if - else- if if (score >= 85) { System.out.println(”Grade is A"); } else { if (score >= 75) { System.out.println(”Grade is B"); if (score >= 65) { System.out.println(”Grade is C"); if (score >= 50) { System.out.println(”Grade is D"); System.out.println(”Grade is N"); } Test Score Grade 85  score A 75  score  85 B 65  score  75 C 50  score  65 D score  50 N If we follow the general rule, the above if-else if will be written as below, but the style shown in the slide is the standard notation. if (score >= 90) messageBox.show("Your grade is A"); else if (score >= 80) messageBox.show("Your grade is B"); if (score >= 70) messageBox.show("Your grade is C"); if (score >= 60) messageBox.show("Your grade is D"); messageBox.show("Your grade is F"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

17 if - else- if Test Score Grade A B C D N 85  score 75  score  85
Chapter 6 if - else- if if (score >= 85) { System.out.println(”Grade is A"); } else if (score >= 75) { System.out.println(”Grade is B"); } else if (score >= 65) { System.out.println(”Grade is C"); } else if (score >= 50) { System.out.println(”Grade is D"); } else { System.out.println(”Grade is N"); } Test Score Grade 85  score A 75  score  85 B 65  score  75 C 50  score  65 D score  50 N If we follow the general rule, the above if-else if will be written as below, but the style shown in the slide is the standard notation. if (score >= 90) messageBox.show("Your grade is A"); else if (score >= 80) messageBox.show("Your grade is B"); if (score >= 70) messageBox.show("Your grade is C"); if (score >= 60) messageBox.show("Your grade is D"); messageBox.show("Your grade is F"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

18 Matching else really means if (x < y) if (x < z)
Chapter 6 Matching else if (x < y) if (x < z) System.out.println("Hello"); else System.out.println("Good bye"); really means if (x < y) { if (x < z) { System.out.println("Hello"); } else { System.out.println("Good bye"); } If you want the else to match with the first if, then you have to write if (x < y) { if (x < z) messageBox.show("Hello"); } else messageBox.show("Good bye"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

19 Matching else means if (x < y) { if (x < z)
Chapter 6 Matching else if (x < y) { if (x < z) System.out.println("Hello"); } else { System.out.println("Good bye"); } means if (x < y) { if (x < z) { System.out.println("Hello"); } } else { System.out.println("Good bye"); If you want the else to match with the first if, then you have to write if (x < y) { if (x < z) messageBox.show("Hello"); } else messageBox.show("Good bye"); August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

20 - The switch Statement …
The switch statement is the only other kind of Java statement that implements multiway branching When a switch statement is evaluated, one of a number of different branches is executed The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch The controlling expression must evaluate to a char, int, short, or byte August 22, 2019 ICS102: The course

21 Syntax for the switch Statement
Chapter 6 Syntax for the switch Statement switch ( <arithmetic expression> ) { <case label 1> : <case body 1> <case label n> : <case body n> } Arithmetic Expression switch ( fanSpeed ) { case 1: System.out.println("That's low"); break; case 2: System.out.println("That's medium"); case 3: System.out.println("That's high"); } Case Label Case Body August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

22 switch With break Statements
Chapter 6 switch With break Statements x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? break; switch ( N ) { case 1: x = 10; break; case 2: x = 20; case 3: x = 30; } August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

23 The switch Statement with default
Chapter 6 The switch Statement with default switch ( <arithmetic expression> ) { <case label 1> : <case body 1> <case label n> : <case body n> default: <default body> } switch ( binaryDigit ) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); default: System.out.println("That's not a binary digit"); } August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

24 switch With No break Statements
Chapter 6 switch With No break Statements x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } August 22, 2019 ICS102: The course Intro to OOP w/Java--Wu

25 … -- The switch Statement
double y = 30; double z = 20; Scanner kb = new Scanner(System.in); System.out.println("1. add "); System.out.println("2. Subtract "); System.out.println("3. Multiply "); System.out.println("Enter a value: between 1 and 3 "); int x = kb.nextInt(); switch (x) { case 1: System.out.println(z + y); break; case 2: System.out.println(z - y); case 3: System.out.println(z * y); default: System.out.println("Wrong Choice."); } August 22, 2019 ICS102: The course

26 THE END August 22, 2019 ICS102: The course


Download ppt "Selection Statements August 22, 2019 ICS102: The course."

Similar presentations


Ads by Google