Chapter 4 Control Structures SELECTION STATEMENTS Chapter Objectives
Chapter 4: Control Structures SELECTION STATEMENTS
Chapter Objectives 2 • • Learn about control structures. Learn how to use the selection control structures if, if…else, and switch in a program. Learn about short-circuit evaluation Learn about loop statements Java Programming: From Problem Analysis to Program Design, D. S. Malik
Control Structures 3 • Three methods of processing a program: – – – • • In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice. Loop: Altering the flow of program execution by repeating statements. Java Programming: From Problem Analysis to Program Design, D. S. Malik
Control Structures 4 Java Programming: From Problem Analysis to Program Design, D. S. Malik
Selection 5 • • • One-way selection Two-way selection Compound (block of) statements Multiple selections (nested if) Conditional operator switch structures Java Programming: From Problem Analysis to Program Design, D. S. Malik
One-Way Selection 6 • Syntax: if (expression) statement • Expression referred to as decision maker. • Statement referred to as action statement. Java Programming: From Problem Analysis to Program Design, D. S. Malik
One-Way Selection Example 1 //Determine the absolute value of an integer import java. util. *; public class Absolute. Value { static Scanner console = new Scanner (System. in); public static void main(String[] args) { int number, temp; System. out. println("Enter an integer: "); number = console. next. Int(); temp = number; if (number < 0) number = -number; System. out. println("The absolute value of " + temp + " is " + number); } } 7 Java Programming: From Problem Analysis to Program Design, D. S. Malik
Short-Circuit Evaluation 8 • • • A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. Example: (x>y) || (x==5) // if (x>y) is true, (x==5) is not evaluated (a==b) && (x>=7) // if (a==b) is false, (x>=7) is not evaluated (x>0) && ( (y = z*2) > 5) // if (x>0) is false, ((y = z*2) > 5) is not evaluated and the value of y will not change Java Programming: From Problem Analysis to Program Design, D. S. Malik
Two-Way Selection 9 • Syntax: if (expression) statement 1 else statement 2 • else statement must be paired with an if. Java Programming: From Problem Analysis to Program Design, D. S. Malik
Two-Way Selection 10 Java Programming: From Problem Analysis to Program Design, D. S. Malik
Two-Way Selection 11 Example 4 -13 if (hours > 40. 0) wages = 40. 0 * rate + 1. 5 * rate * (hours - 40. 0); else wages = hours * rate; Java Programming: From Problem Analysis to Program Design, D. S. Malik
Two-Way Selection 12 Example 4 -14 //The following statement shows an example of a syntax error if (hours > 40. 0); //Line 1 wages = 40. 0 * rate + 1. 5 * rate *(hours - 40. 0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 • Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. • The semicolon at the end of the if statement (see Line 1) ends the if statement. • The statement at Line 2 separates the else clause from the if statement. That is, else is by itself. • Because there is no separate else statement in Java, this code generates a Java Programming: From Problem Analysis to Program Design, syntax error. D. S. Malik
13 Compound (Block of) Statements Syntax: { statement 1 statement 2. . . statementn } Java Programming: From Problem Analysis to Program Design, D. S. Malik
14 Compound (Block of) Statements if (age > 18) { 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. "); }
Multiple Selection: Nested if 15 • Syntax: if (expression 1) statement 1 else if (expression 2) statement 2 else statement 3 • Multiple if statements can be used if there is more than two alternatives • else is associated with the most recent if that does not have an else. Java Programming: From Problem Analysis to Program Design, D. S. Malik
Multiple Selection: Nested if 16 Example 4 -19 // Assume that score is of type int. Based on the value of score, the following code determines the grade if (score >= 90) System. out. println (“Grade is A”); else if (score >=80 ) System. out. println (“Grade is B”); else if (score >=70 ) System. out. println (“Grade is C”); else if (score >=60 ) System. out. println (“Grade is D”); else System. out. println (“Grade is F”); Java Programming: From Problem Analysis to Program Design, D. S. Malik
Multiple Selection: Nested if 17 Example 4 -21 if( tempreture >= 50 ) if (tempreture >= 80) System. out. println (“Good swimming day”); else System. out. println (“Good golfing day”); Java Programming: From Problem Analysis to Program Design, D. S. Malik
Multiple Selection: Nested if 18 Example 4 -20 if( tempreture >= 50 ) if (tempreture >= 80) System. out. println (“Good swimming day”); else System. out. println (“Good golfing day”); else System. out. println (“Good tennis day”); Java Programming: From Problem Analysis to Program Design, D. S. Malik
Multiple Selection: Nested if 19 Example 4 -22 //The following code does not work as intended. For example if GPA=3. 8 if ( GPA >= 2. 0 ) if (GPA >= 3. 9) System. out. println (“Dean Honor list”); else System. out. println (“GPA below graduation requirement”); -------------------------------//This is the correct way of writing the above code if ( GPA >= 2. 0 ) { if (GPA >= 3. 9) System. out. println (“Dean Honor list”); } else System. out. println (“GPA below graduation requirement”); Java Programming: From Problem Analysis to Program Design, D. S. Malik
Conditional (? : ) Operator 20 • Ternary operator • Syntax: expression 1 ? expression 2 : expression 3 • If expression 1 = true, then the result of the condition is expression 2. Otherwise, the result of the condition is expression 3. Java Programming: From Problem Analysis to Program Design, D. S. Malik
Conditional (? : ) Operator 21 Example: int x = 5 , y =3 , min ; if (x< = y) min = x ; else min = y ; The above statement can be written using the conditional operator : min = ( x<= y ) ? Java Programming: From Problem Analysis to Program Design, D. S. Malik x : y ;
Switch Structures 22 switch (expression) { case value 1: statements 1 break; case value 2: statements 2 break; . . . case valuen: statementsn break; default: statements } • Expression is also known as selector. • Expression can be an identifier. • Value can only be integral. Java Programming: From Problem Analysis to Program Design, D. S. Malik
23 Switch With break Statements switch (N) { case 1: x = 10; true N == 1 ? false break; case 2: x = 20; break; case 3: x = 30; break; x = 10; break; true N == 2 ? x = 20; false break; true N == 3 ? false } Java Programming: From Problem Analysis to Program Design, D. S. Malik x = 30; break;
Switch With No break Statements 24 true N == 1 ? x = 10; switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } false true N == 2 ? x = 20; false true N == 3 ? false Java Programming: From Problem Analysis to Program Design, D. S. Malik x = 30;
25 Switch With Break And Default Statements Example 4 -23 switch (grade) { case 'A': System. out. println("The grade break; case 'B': System. out. println("The grade break; case 'C': System. out. println("The grade break; case 'D': System. out. println("The grade break; case 'F': System. out. println("The grade break; default: System. out. println("The grade Java Programming: From Problem Analysis to Program Design, } D. S. Malik is A. "); is B. "); is C. "); is D. "); is F. "); is invalid. ");
26 Switch With Break And Default Statements Example 4 -23 switch (grade) { case 'A': System. out. println("The break; case 'B': System. out. println("The break; case 'C': System. out. println("The break; case 'D': System. out. println("The break; case 'F': System. out. println("The break; default: System. out. println("The } Java Programming: From Problem Analysis to Program Design, D. S. Malik grade is A. "); grade is B. "); grade is C. "); grade is D. "); grade is F. "); grade is invalid. ");
Example 4 -23 With Nested If 27 if (grade == 'A') System. out. println("The grade is A. "); else if (grade == 'B') System. out. println("The grade is B. "); else if (grade == 'C') System. out. println("The grade is C. "); else if (grade == 'D') System. out. println("The grade is D. "); else if (grade == 'F') System. out. println("The grade is F. "); else Java Programming: From Problem Analysis to Program Design, System. out. println("The grade is invalid. "); D. S. Malik
- Slides: 27