Download presentation
Presentation is loading. Please wait.
1
Decision Structures if, if/else conditions
CS Kaminski
2
selection DECISION: determine which of 2 paths to follow
(1+ statements in each path) (ELSE path optional)
3
selection options (in Java)
[“selection”, “guard”, “decision”, “conditional”] Single selection if Double selection if ... else Multiple selection (set of cases) switch or if … else if … else if … else …
4
if (3 variations) if (condition true) action; ------------------
} | if (condition true) { | action1; | | actionN; | } |
5
if … else (3 variations) if (condition true) action1; else action2;
if (condition true){ action1; } else { action2; |if (condition true) { | action1A; | | action7A; |} |else { | action1B; | | action5B;
6
each action could be 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...else or switch a while or do...while or for loop do NOTHING ;
7
the condition comparison (equality & relational) operators:
== != < > <= >= IMPORTANT: == compare for equality = assignment operator compare 2 operands - operands can be: variables, constants, arithmetic expressions, returned values from a method call, but NOT Strings (use different methods to compare them)
8
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
9
logic operators && (and) || (or) ! (not)
( !(age == 25) ) [same as (age != 25)] ( (a < b) && (c < d) ) ( (firstInitial == ‘W’) || (gender == ‘F’) ) [Note: use truth tables to determine results]
10
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)
11
order of precedence NOTE: need ( ) around whole condition
( (a == b) && (c > -14) ) typical (a == b) && (c > -14) WRONG ( a == b && c > -14 ) OK 1st rd nd “All juniors and seniors with a gpa of at least a 3.0” (gpa >= 3.0 && classYear == || classYear == 4) WRONG
12
order of precedence unary operators - + ! arithmetic operators * / %
+ - relational operators < > <= >= equality operators == != logic operators && [and] NOTE: “and” before “or” || [or] assignment operator = but ( ) can over-ride these
13
translate 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
14
actions total = total + exam; counter++;
System.out.println(“blah blah”); num = keyboard.nextInt(); . . .
15
“do nothing” ( ; ) as the action
if (maritalStatus != ‘M’) numNotMarried = numNotMarried + 1; OK, but usually clearer to specify POSITIVE condition vs. NEGATIVE condition if (maritalStatus == ‘M’) ; // empty statement - do nothing else
16
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 RIGHT if (a < b) // no ; here // println MAY happen, depending on if condition
17
nested if/else if (a == 4) // indent/align to show code logic
if (b == 5) // - helps human reader find bugs answer = 1; // - computer ignores it else answer = 2; if (b == 5) answer = 3; answer = 4; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >>
18
empty statement if (a == 4) if (b == 0) ; // do nothing here; it’s OK
else answer = 2; if (b == 2) answer = 3; answer = 4;
19
dangling else ? if (a == 4) if (b == 5) answer = 1;
// WRONG ! Indentation suggests…, but... else // this else is paired with if (b == 5) answer = 3; else answer = 4; NOTE: compiler ignores formatting and does what instructions actually say to do
20
prior example behaves as. . .
if (a == 4) if (b == 5) answer = 1; else // so b != 5 falls here answer = 3; else answer = 4; Trace this code using: a: 4, b: 5, answer >> a: 2, b: 5, answer >> a: 4, b: 2, answer >> a: 2, b: 2, answer >>
21
the FIX for dangling else
if (a == 4) { if (b == 5) answer = 1; } else // else now applies to if (a == 4) answer = 3; else answer = 4;
22
nested if/else if/else . . .
[works, but NOT TYPICAL FORMATTING for category selection] if (total >= 90) System.out.println(‘A’); else if (total >= 80) System.out.println(‘B’); if (total >= 70) System.out.println(‘C’); if (total >= 60) System.out.println(‘D’); System.out.println(‘E’);
23
common formatting if (total >= 90) System.out.println(‘A’);
else if (total >= 80) System.out.println(‘B’); else if (total >= 70) System.out.println(‘C’); else if (total >= 60) System.out.println(‘D’); else System.out.println(‘E’);
24
Stacked (vs. Nested) if’s
int bonus = 0; if (attendancePercent >= 90) bonus = bonus + 5; if (labPoints >= 600) bonus = bonus + 35;
25
stacked - WRONG if (total >= 90) System.out.println(‘A’);
System.out.println(‘B’); if (total >= 70) System.out.println(‘C’); if (total >= 60) System.out.println(‘D’); System.out.println(‘E’);
26
nested vs. stacked NESTED if/else’s
control goes to ONLY 1 block (to block with 1st condition that’s true) 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 1st 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 ALL categories that apply - cumulative bonus points
27
caution on Nested Since control goes to ONLY 1 (or 0) action block
(i.e., block of 1st condition that applies) and no subsequent else if blocks nor the final else block (if any) so ORDER of conditions MAY be important NO for mutually exclusive categories - state YES for “use 1st category that applies” - grade example NOTE: grades are really mutually exclusive, but example didn’t specify ALL necessary conditions for a category
28
switch statement Equivalent to nested if/else Shown later
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.