150 likes | 196 Views
Branching Mechanism. This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials. Flow of Control. Branching mechanisms if-else if switch (break) Looping mechanisms while do while for Boolean expressions
E N D
Branching Mechanism This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials. SWE 510: Object Oriented Programming in Java
Flow of Control • Branching mechanisms • if-else • if • switch (break) • Looping mechanisms • while • do while • for • Boolean expressions • evaluate to either true or false and decide • which branch the control should go to • if the control should stay in a loop SWE 510: Object Oriented Programming in Java
if-else Statement Prev_Statement • Syntax Previous_Statement if ( Boolean_Expression ) Yes_Statement else No_Statement Next_Statement • Examples if ( time < limit ) System.out.println( “You made it” ); else System.out.println( “You missed the deadline” ); if ( side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 > side2) System.out.println( “Forms a triangle” ); else System.out.println( “Does not form a triangle” ); Boolean Expression? true Yes_Statement false No_Statement Next_Statement SWE 510: Object Oriented Programming in Java
Omitting the else Part Prev_Statement • Syntax Previous_Statement if ( Boolean_Expression ) Yes_Statement Action_Statement • Examples if ( weight > ideal ) calorieAllotment = calorieAllotment - 500; if ( mph > 60 ) System.out.println( “Too fast” ); Boolean Expression? true Action_Statement false Next_Statement SWE 510: Object Oriented Programming in Java
Compound Statements • Each Yes_Statement and No_Statementbranch of an if-else can be a made up of a single statement or many statements • Syntax if ( Boolean_Expression ) { Yes_Statement1; Yes_statement2; ...; } else { No_Statement1; No_Statement2; ...; } • Example if ( myScore > yourScore ) { System.out.println( “I win!” ); wager += 100; } else { System.out.println( “I wish these were golf scores” ); wager = 0; } Yes_Statement1 Yes_Statement2 … Boolean Expression? true false No_Statement1 No_Statement2 … SWE 510: Object Oriented Programming in Java
Equivalent Nested Statements • if-else statements and if statements both contain smaller statements within them • Example if ( side1 == side2 ) { System.out.println( “Isoceles” ); if ( side2 == side3 ) System.out.println( “Equilateral” ); } if ( side1 == side2 ) System.out.println( “Isoceles” ); if ( side1 == side2 && side2 == side3 ( System.out.println( “Equilateral” ); SWE 510: Object Oriented Programming in Java
Multiway if-else Statement • Syntax if (Boolean_Expression_1) Statement_1 else if (Boolean_Expression_2) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities • Example if ( testScore >= 95 ) grade = ‘A’; else if ( testScore >= 85 ) grade = ‘B’; else if ( testScore >= 75 ) grade = ‘C’; else if ( testScore >= 65 ) grade = ‘D’; else grade = ‘F’; System.out.println( “grade = ” + grade ); Expression_1? Statement_1 Expression_2? Statement_2 Expression_n? Statement_n Other_Statement SWE 510: Object Oriented Programming in Java
What’s Redundant? • Improve Textbook p101’s code if ( netIncome <= 15000 ) tax = 0; else if ( ( netIncome > 15000 ) && ( netIncome <= 30000 ) ) tax = 0.05 * ( netIncome – 15000 ); else { fivePercentTax = 0.05 * 15000; tenPercentTax = 0.10 * ( netIncome – 30000 ); tax = fivePercentTax + tenPercentTax; } true netIncome <= 15000 Tax = 0 false true netIncome <= 30000 Tax = 0.05 * … false Tax = 0.05 * …+ 0.10 * ….; SWE 510: Object Oriented Programming in Java
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 • Syntax switch (Controlling_Expression) { case Case_Label_1: Statement_Sequence_1 break; case Case_Label_2: Statement_Sequence_2 break; case Case_Label_n: Statement_Sequence_n break; default: Default_Statement Sequence break; } Must be a constant: char, int, short, or byte Controlling_Exp Label_1? true Statement_1 false Label_2? Statement_2 Label_n? Statement_n default_Statement Optional but recommended Executed in none of label_1 ~ n SWE 510: Object Oriented Programming in Java
Example 1 (Textbook p103) int vehicleClass; double toll; ...; switch ( vehicleClass ) { case 1: System.out.println( “Passenger car.” ); toll = 0.50; break; case 2: System.out.println( “Bus.” ); toll = 1.50; break; case 3: System.out.println( “Truck.” ); toll = 2.00; break; default: System.out.println( “Unknown vehicle class!” ); break; } A controlling expression must return char, int, short, or byte. Case labels must be a constant. Recommended to detect error cases. SWE 510: Object Oriented Programming in Java
Example 2 (java.sun.com tutorial) A controlling expression must return char, int, short, or byte. class SwitchDemo { 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; default: System.out.println("Invalid month."); break; } } } Case labels must be a constant. Recommended to detect error cases. SWE 510: Object Oriented Programming in Java
switch Statement without breaks • Syntax switch (Controlling_Expression) { case Case_Label_1: Statement_Sequence_1 case Case_Label_2: Statement_Sequence_2 break; case Case_Label_m; Statement_Sequence_m case Case_Label_n: Statement_Sequence_n break; default: Default_Statement Sequence break; } Controlling_Exp Label_1? true Statement_1 false Label_2? Statement_2 Label_m? Statement_m Label_n? Statement_n Default_stmt SWE 510: Object Oriented Programming in Java
Example 3 (java.sun.com tutorial) class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays); } } SWE 510: Object Oriented Programming in Java
PITFALL: Forgetting a break class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; case 4: case 6: case 9: case 11: numDays = 30; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0) ) numDays = 29; else numDays = 28; default: System.out.println("Invalid month."); } System.out.println("Number of Days = " + numDays); } } • Compiler does not complain about no breaks. • Control continues down to the next case. SWE 510: Object Oriented Programming in Java
Equivalent Conditional Operator • A notational variant on certain forms of the if-else statement • Also called the ternary operator or arithmetic if • Syntax ( Boolean_Expression ) ? Yes_Expression : No_Expression; // if Boolean_Expression is true, evaluate Yes_Expression, // otherwise evaluate No_Expression, and finally return the // resulted value. • Example max = ( n1 > n2 ) ? n1 : n2; if ( n1 > n2 ) max = n1; else max = n2; SWE 510: Object Oriented Programming in Java