Card 09/ 09
Recap: Conditionals and Loops
The two conditional forms, the four loops, and the two boundary mistakes, with where each was.
Conditionals and loops in Java come down to the seven ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
An if ladder tests a condition per rung; a switch matches one value | Conditionals in Java: if and switch |
| In a ladder, the first true condition wins, so the order of the rungs matters | Conditionals Example: A Letter Grade With if and else if |
If every rung compares the same variable with a constant, write a switch | if vs switch: Choosing by What the Branches Test |
| Four loop forms, separated by how much you know before the loop starts | Loops in Java: for, Enhanced for, while and do-while |
A counter from 0 compares with <; a counter from 1 compares with <= | Off-by-One Errors in Loop Conditions |
The colon form of switch picks a starting point, not a branch | Fall-Through in a switch Without break |
break ends the loop; continue ends the pass | Practice: Predicting What break and continue Do |
In both halves of this topic the bug lives at the boundary rather than in the body. A counter that is one out and a missing break are the same kind of mistake, because in both the code runs one step further than it reads.
← → move through sections and cards