Card 09/ 09

RecapDifficulty: Intermediate1 min

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.

What the topic established, and which card it was on
The ideaWhere
An if ladder tests a condition per rung; a switch matches one valueConditionals in Java: if and switch
In a ladder, the first true condition wins, so the order of the rungs mattersConditionals Example: A Letter Grade With if and else if
If every rung compares the same variable with a constant, write a switchif vs switch: Choosing by What the Branches Test
Four loop forms, separated by how much you know before the loop startsLoops 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 branchFall-Through in a switch Without break
break ends the loop; continue ends the passPractice: 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.