CS101 · Chapter 3 · Annotated SlidesSelections
Original
Overview

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.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 62 slides
  1. 1 Selections
  2. 2 Motivations
  3. 3 § To declare boolean operators ( § 3.2). § To implement selection control using one-way statements ( § 3.3).
  4. 4 boolean The Type and Operators
  5. 5 Relational Operators
  6. 6 Problem: A Simple Math Learning Tool
  7. 7 if
  8. 8 Note
  9. 9 Simple if Demo
  10. 10 if
  11. 11 if-else
  12. 12 Multiple Alternative if Statements
  13. 13 Multi-Way if-else Statements
  14. 14 animation
  15. 15 animation
  16. 16 animation
  17. 17 animation
  18. 18 animation
  19. 19 Note
  20. 20 Note, cont.
  21. 21 Common Errors
  22. 22 TIP
  23. 23 CAUTION
  24. 24 Problem: An Improved Math Learning Tool
  25. 25 Problem: Body Mass Index
  26. 26 Problem: Computing Taxes
  27. 27 Problem: Computing Taxes, cont.
  28. 28 Logical Operators
  29. 29 Truth Table for Operator !
  30. 30 Truth Table for Operator &&
  31. 31 Truth Table for Operator ||
  32. 32 Truth Table for Operator ^
  33. 33 Examples
  34. 34 Examples
  35. 35 Companion Website
  36. 36 Companion Website
  37. 37 Problem: Determining Leap Year?
  38. 38 Problem: Lottery
  39. 39 switch
  40. 40 switch
  41. 41 switch
  42. 42 switch
  43. 43 animation
  44. 44 animation
  45. 45 animation
  46. 46 animation
  47. 47 animation
  48. 48 animation
  49. 49 animation
  50. 50 Problem: Chinese Zodiac
  51. 51 Conditional Expressions
  52. 52 Conditional Operator
  53. 53 Conditional Operator, cont.
  54. 54 Operator Precedence
  55. 55 Operator Precedence and Associativity
  56. 56 Operator Associativity
  57. 57 Example
  58. 58 Companion
  59. 59 Debugging
  60. 60 Debugger
  61. 61 Debugging in NetBeans
  62. 62 Debugging in Eclipse
Slide 1 / 62

Selections

Slide 1: Selections
Slide 2 / 62

Motivations

Slide 2: Motivations

Notes

  1. Programs need to choose between paths: selection statements.

Slide 3 / 62

§ To declare boolean operators ( § 3.2). § To implement selection control using one-way statements ( § 3.3).

Slide 3: § To declare boolean operators ( § 3.2). § To implement selection control using one-way statements ( § 3.3).
Slide 4 / 62

boolean The Type and Operators

Slide 4: boolean The Type and Operators
Slide 5 / 62

Relational Operators

Slide 5: Relational Operators

Notes

  1. Trap = assigns; == compares.

Exam angle

PredictionQ1

Which operator tests equality?

Slide 6 / 62

Problem: A Simple Math Learning Tool

Slide 6: Problem: A Simple Math Learning Tool
Slide 7 / 62

if

Slide 7: if

Notes

  1. One-way if: run the block only when the condition is true.

Slide 8 / 62

Note

Slide 8: Note

Notes

  1. The condition must be in parentheses; braces can be omitted for a single statement.

Slide 9 / 62

Simple if Demo

Slide 9: Simple if Demo

Notes

  1. Example: even/odd with if.

Slide 10 / 62

if

Slide 10: if

Notes

  1. if flowchart.

Slide 11 / 62

if-else

Slide 11: if-else

Notes

  1. if-else: two paths.

Slide 12 / 62

Multiple Alternative if Statements

Slide 12: Multiple Alternative if Statements
Slide 13 / 62

Multi-Way if-else Statements

Slide 13: Multi-Way if-else Statements

Notes

  1. Multi-way if-else: check conditions in order; the first true one runs.

Exam angle

PredictionQ1

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

Slide 14 / 62

animation

Slide 14: animation

Notes

  1. Trace of the multi-way if.

Slide 15 / 62

animation

Slide 15: animation

Notes

  1. Trace.

Slide 16 / 62

animation

Slide 16: animation

Notes

  1. Trace.

Slide 17 / 62

animation

Slide 17: animation

Notes

  1. Trace.

Slide 18 / 62

animation

Slide 18: animation

Notes

  1. Trace.

Slide 19 / 62

Note

Slide 19: Note

Notes

  1. An else matches the NEAREST unmatched if in the same block (dangling else).

Exam angle

PredictionQ1

An else always matches the first if in the code.

Slide 20 / 62

Note, cont.

Slide 20: Note, cont.
Slide 21 / 62

Common Errors

Slide 21: Common Errors

Notes

  1. Common error: a semicolon after if (radius >= 0); makes an empty body.

Exam angle

PredictionQ1

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

Compare with the model answer

The semicolon ends the if with an empty statement, so the block always runs regardless of the condition.

Slide 22 / 62

TIP

Slide 22: TIP
Slide 23 / 62

CAUTION

Slide 23: CAUTION
Slide 24 / 62

Problem: An Improved Math Learning Tool

Slide 24: Problem: An Improved Math Learning Tool
Slide 25 / 62

Problem: Body Mass Index

Slide 25: Problem: Body Mass Index

Notes

  1. BMI = weight / height²; categories by multi-way if.

Slide 26 / 62

Problem: Computing Taxes

Slide 26: Problem: Computing Taxes
Slide 27 / 62

Problem: Computing Taxes, cont.

Slide 27: Problem: Computing Taxes, cont.

Notes

  1. Taxes continued.

Slide 28 / 62

Logical Operators

Slide 28: Logical Operators
Slide 29 / 62

Truth Table for Operator !

Slide 29: Truth Table for Operator !
Slide 30 / 62

Truth Table for Operator &&

Slide 30: Truth Table for Operator &&
Slide 31 / 62

Truth Table for Operator ||

Slide 31: Truth Table for Operator ||

Notes

  1. || is true if at least one is true.

Slide 32 / 62

Truth Table for Operator ^

Slide 32: Truth Table for Operator ^

Notes

  1. ^ is true if exactly one is true.

Exam angle

PredictionQ1

true ^ true evaluates to:

Slide 33 / 62

Examples

Slide 33: Examples
Slide 34 / 62

Examples

Slide 34: Examples

Notes

  1. More examples.

Slide 35 / 62

Companion Website

Slide 35: Companion Website

Notes

  1. Short-circuit: && stops if the left side is false; || stops if the left side is true.

Exam angle

PredictionQ1

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

Slide 36 / 62

Companion Website

Slide 36: Companion Website

Notes

  1. Short-circuit continued.

Slide 37 / 62

Problem: Determining Leap Year?

Slide 37: Problem: Determining Leap Year?

Exam angle

PredictionQ1

Is 1900 a leap year?

Slide 38 / 62

Problem: Lottery

Slide 38: Problem: Lottery
Slide 39 / 62

switch

Slide 39: switch

Notes

  1. switch on an int, char, String or enum; each case ends with break.

Slide 40 / 62

switch

Slide 40: switch

Notes

  1. Without break, execution falls through to the next case.

  2. Trap Forgetting break makes the following cases run too (fall-through).

Exam angle

PredictionQ1

What happens if you omit break in a switch case?

Compare with the model answer

Execution falls through and the statements of the following cases run as well, until a break or the end of the switch.

Slide 41 / 62

switch

Slide 41: switch
Slide 42 / 62

switch

Slide 42: switch

Notes

  1. switch flowchart.

Slide 43 / 62

animation

Slide 43: animation
Slide 44 / 62

animation

Slide 44: animation

Notes

  1. Trace.

Slide 45 / 62

animation

Slide 45: animation

Notes

  1. Trace.

Slide 46 / 62

animation

Slide 46: animation

Notes

  1. Trace.

Slide 47 / 62

animation

Slide 47: animation

Notes

  1. Trace.

Slide 48 / 62

animation

Slide 48: animation

Notes

  1. Trace.

Slide 49 / 62

animation

Slide 49: animation

Notes

  1. Trace.

Slide 50 / 62

Problem: Chinese Zodiac

Slide 50: Problem: Chinese Zodiac
Slide 51 / 62

Conditional Expressions

Slide 51: Conditional Expressions
Slide 52 / 62

Conditional Operator

Slide 52: Conditional Operator

Notes

  1. boolean-expression ? exp1 : exp2.

Exam angle

PredictionQ1

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

Slide 53 / 62

Conditional Operator, cont.

Slide 53: Conditional Operator, cont.

Notes

  1. Example: print even/odd with the conditional operator.

Slide 54 / 62

Operator Precedence

Slide 54: Operator Precedence

Notes

  1. Precedence (high → low): ++/-- , ! , * / % , + − , < <= > >= , == != , ^ , && , || , = op=.

Slide 55 / 62

Operator Precedence and Associativity

Slide 55: Operator Precedence and Associativity
Slide 56 / 62

Operator Associativity

Slide 56: Operator Associativity
Slide 57 / 62

Example

Slide 57: Example

Notes

  1. Precedence example worked step by step.

Slide 58 / 62

Companion

Slide 58: Companion

Notes

  1. Companion material.

Slide 59 / 62

Debugging

Slide 59: Debugging
Slide 60 / 62

Debugger

Slide 60: Debugger

Notes

  1. A debugger lets you step through code, set breakpoints and watch variables.

Slide 61 / 62

Debugging in NetBeans

Slide 61: Debugging in NetBeans
Slide 62 / 62

Debugging in Eclipse

Slide 62: Debugging in Eclipse