- Programs need to choose between paths: selection statements.
- Objectives: boolean, if, if-else, nested if, logical operators, switch, conditional operator, precedence.
- boolean holds true or false; relational operators produce booleans.
- Relational operators: < <= > >= == !=.
🎯 How this can come up · 1 question
Which operator tests equality?
Correct: ==.
- Math learning tool: random addition question, check the answer with a boolean.
- One-way if: run the block only when the condition is true.
- The condition must be in parentheses; braces can be omitted for a single statement.
- Example: even/odd with if.
- if flowchart.
- if-else: two paths.
- Nested and multiple alternative if statements.
- Multi-way if-else: check conditions in order; the first true one runs.
🎯 How this can come up · 1 question
score = 85: if (score >= 90) A; else if (score >= 80) B; else if (score >= 70) C. Which grade?
Correct: B.
- Trace of the multi-way if.
- Trace.
- Trace.
- Trace.
- Trace.
- An else matches the NEAREST unmatched if in the same block (dangling else).
🎯 How this can come up · 1 question
An else always matches the first if in the code.
False. It matches the nearest unmatched if.
- Use braces to force the matching you want.
- Common error: a semicolon after if (radius >= 0); makes an empty body.
🎯 How this can come up · 1 question
What is wrong with: if (radius >= 0); { area = radius * radius * PI; }
The semicolon ends the if with an empty statement, so the block always runs regardless of the condition.
- Tip: assign a boolean directly: boolean even = number % 2 == 0;
- Caution: write if (even), not if (even == true).
- Improved math tool with subtraction.
- BMI = weight / height²; categories by multi-way if.
- Computing taxes by filing status and brackets.
- Taxes continued.
- Logical operators: ! (not), && (and), || (or), ^ (exclusive or).
- ! flips true/false.
- && is true only if both are true.
- || is true if at least one is true.
- ^ is true if exactly one is true.
🎯 How this can come up · 1 question
true ^ true evaluates to:
Correct: false.
- Examples: divisible by 2 and 3 (&&), by 2 or 3 (||), by 2 or 3 but not both (^).
- More examples.
- Short-circuit: && stops if the left side is false; || stops if the left side is true.
🎯 How this can come up · 1 question
In (x != 0 && 10 / x > 1), the division is not evaluated when x is 0.
True. Short-circuit evaluation.
- Short-circuit continued.
- Leap year: (year % 4 0 && year % 100 != 0) || (year % 400 0).
🎯 How this can come up · 1 question
Is 1900 a leap year?
Divisible by 100 but not 400.
- Lottery: compare a random two-digit number with the user’s, matching digits in or out of order.
- switch on an int, char, String or enum; each case ends with break.
- Without break, execution falls through to the next case.
🎯 How this can come up · 1 question
What happens if you omit break in a switch case?
Execution falls through and the statements of the following cases run as well, until a break or the end of the switch.
- default runs when no case matches.
- switch flowchart.
- Trace of a switch.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Trace.
- Chinese zodiac: year % 12 picks the animal with a switch.
- Conditional expression: y = (x > 0) ? 1 : -1;
- boolean-expression ? exp1 : exp2.
🎯 How this can come up · 1 question
What is max = (a > b) ? a : b; when a = 3, b = 7?
Correct: 7.
- Example: print even/odd with the conditional operator.
- Precedence (high → low): ++/-- , ! , * / % , + − , < <= > >= , == != , ^ , && , || , = op=.
- Associativity: binary operators left to right; assignment right to left.
- a = b += c = 5 evaluates right to left.
- Precedence example worked step by step.
- Companion material.
- Debugging: find and fix logic errors.
- A debugger lets you step through code, set breakpoints and watch variables.
- Debugging in NetBeans.
- Debugging in Eclipse.
No slides match this filter.