Decision Structures if, if/ conditions Selection DECISION: determine which of 2 paths to follow (1+ statements in each path) CS1110 - Kaminski (ELSE path optional) 2 Selection options (in Java) Plain if s (3 variations) [ selection, guard, decision, conditional ] Single selection Double selection Multiple selection switch if if... or if if if if (condition true) action; ------------------ if (condition true) { action; if (condition true) { action1; actionn; 3 4 if (3 variations) Each action could be any of: if (condition true) action1; action2; ------------------ if (condition true){ action1; { action2; if (condition true) { action1a; action7a; { action1b; action5b; 5 A simple action: Assignment statement with arithmetic expression or method call I/O from keyboard / file / window Call to another method Another selection statement: if or if... or switch A while or do...while or for loop or, do absolutely NOTHING ; 6 1
The Condition Comparison (equality & relational) operators: ==!= < > <= >= IMPORTANT: == compare for equality = the assignment operator Compare 2 operands - operands can be: variables, constants, arithmetic expressions, returned values from a method call, 7 But NOT Strings (different methods to compare them) A Condition is true or false (ageofstudent < MI_DRINKING_AGE) (age!= 21) (michiganresident) // a boolean variable ( (a + 2 * 3) <= ( (b - 4) % 3) ) ( (Math.PI * r * r) < maxsize ) NOTE: need ( ) around whole condition 8 Logic operators in conditions && (and) (or)! (not) (!(age == 25) ) [same as (age!= 25)] ( (a < b) && (c < d) ) ( (firstinitial == W ) (gender == F ) ) Truth Table c1 c2 c1 && c2 c1 c2!c1!c2 F F F F T T F T F T T F T F F T F T T T T T F F e.g., c1: (age >= 21) c2: (classyear = 1) [Note: use truth tables to determine results] 9 10 Order of precedence? NOTE: need ( ) around whole condition ( (a == b) && (c > -14) ) typical (a == b) && (c > -14) WRONG ( a == b && c > -14 ) OK 1 st 3 rd 2 nd All juniors and seniors with a gpa of at least a 3.0 (gpa >= 3.0 && classyear == 3 classyear == 4) WRONG Order of precedence of operators unary operators - +! arithmetic operators * / % + - relational operators < > <= >= equality operators ==!= logic operators && [and] NOTE: and before or assignment operator = [or] 11 but ( ) can over-ride these 12 2
Translation from English? All juniors and seniors should get bonus points if (status == 3 && status == 4) { WRONG NOTE: status variable holds ONE value if (status == 3 status == 4) { RIGHT 13 Actions total = total + exam; counter++; System.out.println( blah blah ); num = keyboard.nextint(); 14 do nothing ( ; )Action if (maritalstatus!= M ) numnotmarried = numnotmarried + 1; OK, but if it s clearer (less bug-prone) to specify POSITIVE condition vs. NEGATIVE condition Caution with ; (it means Empty block of actions ) WRONG if (a < b); System.out.println( a < b ); // println will ALWAYS happen; not related to if if (maritalstatus == M ) ; // empty statement - do nothing numnotmarried = numnotmarried + 1; 15 RIGHT if (a < b) // no ; here System.out.println( a < b ); // println MAY happen, depending on if condition 16 Nested if/ Empty statement // note indent/align formatting answer = 2; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> 17 a: 4, b: 2, answer >> a: 2, b: 2, answer >> if (b == 0) ; // do nothing here, OK answer = 2; if (b == 2) 18 3
Dangling? // WRONG? Indentation suggests, but... // this paired with NOTE: compiler ignores formatting and does what instructions actually say to do 19 Prior example actually says : // so b!= 5 falls here Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >> 20 Dangling - the FIX { // now applies to 21 Nested if/ if/ [works, but NOT TYPICAL FORMATTING] if (total >= 80) if (total >= 70) if (total >= 60) 22 Nested typical formatting: if (total >= 80) if (total >= 70) if (total >= 60) Stacked (vs. Nested) if s int bonus = 0; if (attendancepercent >= 90) bonus = bonus + 5; if (labpoints >= 600) bonus = bonus + 35; 23 24 4
Stacked - WRONG if (total >= 80) if (total >= 70) if (total >= 60) 25 Nested vs. Stacked guidance NESTED if/ s control goes to ONLY 1 block (to 1 st condition that s true block) so ONLY 1 set of actions is done (or none) ~ GUI radio buttons used for: mutually exclusive categories - which state to use for tax rate FIRST category that applies - grades example STACKED if s control goes to ALL blocks (& checks all conditions), so ALL/many sets of actions MIGHT be done ~ GUI check boxes used for: ALL categories that apply - cumulative bonus points 26 Caution on Nested Since control goes to ONLY 1 (or 0) action block (i.e., the 1 st condition that applies) and no subsequent if blocks nor the final block (if any) Switch statement Equivalent to nested if/ Shown later so ORDER of conditions MAY be important NO for mutually exclusive categories - state YES for use 1 st category that applies - grade example NOTE: grades are really mutually exclusive, but example didn t specify ALL conditions for a category 27 28 5