CS101 · Annotated SlidesChapter 3 — Selections
Chapter 3 · Annotated slides

Selections

The boolean type and relational operators, one-way, two-way, nested and multi-way if statements, common if mistakes, logical operators and truth tables, switch, the conditional (ternary) operator, operator precedence and associativity, and debugging. Worked problems: math quiz, BMI, taxes, leap year, lottery, Chinese zodiac.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

Selections

CS101 · Chapter 3
1
Slide 2: Motivations
Exam weight Low
  1. Programs need to choose between paths: selection statements.
Slide 3: § To declare boolean operators ( § 3.2). § To implement selection control using one-way statements ( § 3.3).
Exam weight Low
  1. Objectives: boolean, if, if-else, nested if, logical operators, switch, conditional operator, precedence.
Slide 4: boolean The Type and Operators
Exam weight Low
  1. boolean holds true or false; relational operators produce booleans.
Slide 5: Relational Operators
Exam weight High
  1. Relational operators: < <= > >= == !=.
Exam trap= assigns; == compares.
🎯 How this can come up · 1 question
MCQ

Which operator tests equality?

Slide 6: Problem: A Simple Math Learning Tool
Exam weight Low
  1. Math learning tool: random addition question, check the answer with a boolean.
Slide 7: if
Exam weight Low
  1. One-way if: run the block only when the condition is true.
Slide 8: Note
Exam weight Low
  1. The condition must be in parentheses; braces can be omitted for a single statement.
Slide 9: Simple if Demo
Exam weight Low
  1. Example: even/odd with if.
Slide 10: if
Exam weight Low
  1. if flowchart.
Slide 11: if-else
Exam weight Low
  1. if-else: two paths.
Slide 12: Multiple Alternative if Statements
Exam weight Low
  1. Nested and multiple alternative if statements.
Slide 13: Multi-Way if-else Statements
Exam weight High
  1. Multi-way if-else: check conditions in order; the first true one runs.
🎯 How this can come up · 1 question
MCQ

score = 85: if (score >= 90) A; else if (score >= 80) B; else if (score >= 70) C. Which grade?

Slide 14: animation
Exam weight Low
  1. Trace of the multi-way if.
Slide 15: animation
Exam weight Low
  1. Trace.
Slide 16: animation
Exam weight Low
  1. Trace.
Slide 17: animation
Exam weight Low
  1. Trace.
Slide 18: animation
Exam weight Low
  1. Trace.
Slide 19: Note
Exam weight High
  1. An else matches the NEAREST unmatched if in the same block (dangling else).
🎯 How this can come up · 1 question
True / False

An else always matches the first if in the code.

Slide 20: Note, cont.
Exam weight Low
  1. Use braces to force the matching you want.
Slide 21: Common Errors
Exam weight High
  1. Common error: a semicolon after if (radius >= 0); makes an empty body.
🎯 How this can come up · 1 question
Written

What is wrong with: if (radius >= 0); { area = radius * radius * PI; }

Slide 22: TIP
Exam weight Low
  1. Tip: assign a boolean directly: boolean even = number % 2 == 0;
Slide 23: CAUTION
Exam weight Low
  1. Caution: write if (even), not if (even == true).
Slide 24: Problem: An Improved Math Learning Tool
Exam weight Low
  1. Improved math tool with subtraction.
Slide 25: Problem: Body Mass Index
Exam weight Low
  1. BMI = weight / height²; categories by multi-way if.
Slide 26: Problem: Computing Taxes
Exam weight Low
  1. Computing taxes by filing status and brackets.
Slide 27: Problem: Computing Taxes, cont.
Exam weight Low
  1. Taxes continued.
Slide 28: Logical Operators
Exam weight Low
  1. Logical operators: ! (not), && (and), || (or), ^ (exclusive or).
Slide 29: Truth Table for Operator !
Exam weight Low
  1. ! flips true/false.
Slide 30: Truth Table for Operator &&
Exam weight Low
  1. && is true only if both are true.
Slide 31: Truth Table for Operator ||
Exam weight Low
  1. || is true if at least one is true.
Slide 32: Truth Table for Operator ^
Exam weight Medium
  1. ^ is true if exactly one is true.
🎯 How this can come up · 1 question
MCQ

true ^ true evaluates to:

Slide 33: Examples
Exam weight Low
  1. Examples: divisible by 2 and 3 (&&), by 2 or 3 (||), by 2 or 3 but not both (^).
Slide 34: Examples
Exam weight Low
  1. More examples.
Slide 35: Companion Website
Exam weight High
  1. Short-circuit: && stops if the left side is false; || stops if the left side is true.
🎯 How this can come up · 1 question
True / False

In (x != 0 && 10 / x > 1), the division is not evaluated when x is 0.

Slide 36: Companion Website
Exam weight Low
  1. Short-circuit continued.
Slide 37: Problem: Determining Leap Year?
Exam weight High
  1. Leap year: (year % 4 0 && year % 100 != 0) || (year % 400 0).
🎯 How this can come up · 1 question
MCQ

Is 1900 a leap year?

Slide 38: Problem: Lottery
Exam weight Low
  1. Lottery: compare a random two-digit number with the user’s, matching digits in or out of order.
Slide 39: switch
Exam weight Low
  1. switch on an int, char, String or enum; each case ends with break.
Slide 40: switch
Exam weight High
  1. Without break, execution falls through to the next case.
Exam trapForgetting break makes the following cases run too (fall-through).
🎯 How this can come up · 1 question
Written

What happens if you omit break in a switch case?

Slide 41: switch
Exam weight Low
  1. default runs when no case matches.
Slide 42: switch
Exam weight Low
  1. switch flowchart.
Slide 43: animation
Exam weight Low
  1. Trace of a switch.
Slide 44: animation
Exam weight Low
  1. Trace.
Slide 45: animation
Exam weight Low
  1. Trace.
Slide 46: animation
Exam weight Low
  1. Trace.
Slide 47: animation
Exam weight Low
  1. Trace.
Slide 48: animation
Exam weight Low
  1. Trace.
Slide 49: animation
Exam weight Low
  1. Trace.
Slide 50: Problem: Chinese Zodiac
Exam weight Low
  1. Chinese zodiac: year % 12 picks the animal with a switch.
Slide 51: Conditional Expressions
Exam weight Low
  1. Conditional expression: y = (x > 0) ? 1 : -1;
Slide 52: Conditional Operator
Exam weight High
  1. boolean-expression ? exp1 : exp2.
🎯 How this can come up · 1 question
MCQ

What is max = (a > b) ? a : b; when a = 3, b = 7?

Slide 53: Conditional Operator, cont.
Exam weight Low
  1. Example: print even/odd with the conditional operator.
Slide 54: Operator Precedence
Exam weight Medium
  1. Precedence (high → low): ++/-- , ! , * / % , + − , < <= > >= , == != , ^ , && , || , = op=.
Slide 55: Operator Precedence and Associativity
Exam weight Low
  1. Associativity: binary operators left to right; assignment right to left.
Slide 56: Operator Associativity
Exam weight Low
  1. a = b += c = 5 evaluates right to left.
Slide 57: Example
Exam weight Low
  1. Precedence example worked step by step.
Slide 58: Companion
Exam weight Low
  1. Companion material.
Slide 59: Debugging
Exam weight Low
  1. Debugging: find and fix logic errors.
Slide 60: Debugger
Exam weight Low
  1. A debugger lets you step through code, set breakpoints and watch variables.
Slide 61: Debugging in NetBeans
Exam weight Low
  1. Debugging in NetBeans.
Slide 62: Debugging in Eclipse
Exam weight Low
  1. Debugging in Eclipse.