Which operator tests equality?
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 Selections
- 2 Motivations
- 3 § To declare boolean operators ( § 3.2). § To implement selection control using one-way statements ( § 3.3).
- 4 boolean The Type and Operators
- 5 Relational Operators
- 6 Problem: A Simple Math Learning Tool
- 7 if
- 8 Note
- 9 Simple if Demo
- 10 if
- 11 if-else
- 12 Multiple Alternative if Statements
- 13 Multi-Way if-else Statements
- 14 animation
- 15 animation
- 16 animation
- 17 animation
- 18 animation
- 19 Note
- 20 Note, cont.
- 21 Common Errors
- 22 TIP
- 23 CAUTION
- 24 Problem: An Improved Math Learning Tool
- 25 Problem: Body Mass Index
- 26 Problem: Computing Taxes
- 27 Problem: Computing Taxes, cont.
- 28 Logical Operators
- 29 Truth Table for Operator !
- 30 Truth Table for Operator &&
- 31 Truth Table for Operator ||
- 32 Truth Table for Operator ^
- 33 Examples
- 34 Examples
- 35 Companion Website
- 36 Companion Website
- 37 Problem: Determining Leap Year?
- 38 Problem: Lottery
- 39 switch
- 40 switch
- 41 switch
- 42 switch
- 43 animation
- 44 animation
- 45 animation
- 46 animation
- 47 animation
- 48 animation
- 49 animation
- 50 Problem: Chinese Zodiac
- 51 Conditional Expressions
- 52 Conditional Operator
- 53 Conditional Operator, cont.
- 54 Operator Precedence
- 55 Operator Precedence and Associativity
- 56 Operator Associativity
- 57 Example
- 58 Companion
- 59 Debugging
- 60 Debugger
- 61 Debugging in NetBeans
- 62 Debugging in Eclipse
Motivations
Notes
Programs need to choose between paths: selection statements.
Relational Operators
Notes
Trap = assigns; == compares.
Exam angle
if
Notes
One-way if: run the block only when the condition is true.
Note
Notes
The condition must be in parentheses; braces can be omitted for a single statement.
Simple if Demo
Notes
Example: even/odd with if.
if
Notes
if flowchart.
if-else
Notes
if-else: two paths.
Multi-Way if-else Statements
Notes
Multi-way if-else: check conditions in order; the first true one runs.
Exam angle
score = 85: if (score >= 90) A; else if (score >= 80) B; else if (score >= 70) C. Which grade?
animation
Notes
Trace of the multi-way if.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Note
Notes
An else matches the NEAREST unmatched if in the same block (dangling else).
Exam angle
An else always matches the first if in the code.
- It matches the nearest unmatched if.
Common Errors
Notes
Common error: a semicolon after if (radius >= 0); makes an empty body.
Exam angle
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.
Problem: Body Mass Index
Notes
BMI = weight / height²; categories by multi-way if.
Problem: Computing Taxes, cont.
Notes
Taxes continued.
Truth Table for Operator ||
Notes
|| is true if at least one is true.
Truth Table for Operator ^
Notes
^ is true if exactly one is true.
Exam angle
true ^ true evaluates to:
Examples
Notes
More examples.
Companion Website
Notes
Short-circuit: && stops if the left side is false; || stops if the left side is true.
Exam angle
In (x != 0 && 10 / x > 1), the division is not evaluated when x is 0.
- Short-circuit evaluation.
Companion Website
Notes
Short-circuit continued.
Problem: Determining Leap Year?
Exam angle
Is 1900 a leap year?
- Divisible by 100 but not 400.
switch
Notes
switch on an int, char, String or enum; each case ends with break.
switch
Notes
Without break, execution falls through to the next case.
Trap Forgetting break makes the following cases run too (fall-through).
Exam angle
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.
switch
Notes
switch flowchart.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
animation
Notes
Trace.
Conditional Operator
Notes
boolean-expression ? exp1 : exp2.
Exam angle
What is max = (a > b) ? a : b; when a = 3, b = 7?
Conditional Operator, cont.
Notes
Example: print even/odd with the conditional operator.
Operator Precedence
Notes
Precedence (high → low): ++/-- , ! , * / % , + − , < <= > >= , == != , ^ , && , || , = op=.
Example
Notes
Precedence example worked step by step.
Companion
Notes
Companion material.
Debugger
Notes
A debugger lets you step through code, set breakpoints and watch variables.