Unit 4 · Thinking like a programmer
< vs <=: getting boundaries right in code
“Under 5” means 0 to 4, so write age < 5. “65 and over” includes 65, so write age >= 65. Test the boundary value itself, because that is where these bugs live.
The mix-up
What people often think
“"Under 5" includes 5.”
What the tutor says
Under 5 means 0 to 4. age <= 5 would let a five-year-old in free, so use age < 5.
How it works
< and > leave the number out; <= and >= include it. age <= 5 would let a five-year-old in free, and age > 65 would leave out someone who just turned 65.
The fastest check is to try the exact number in the rule: 5, 65, 18, 50. If the code and the rule disagree on that one value, you have found the bug.
An example
const free = age < 5; // 0 to 4
const senior = age >= 65; // 65 and overClose cousins of this mix-up
The tutor watches for these in the same lesson.
“"65 and over" means more than 65.”
"And over" includes 65 itself. age > 65 leaves out someone who just turned 65, so use age >= 65.
Practise it with a tutor watching
Conditionals: making decisions
Turn a decision into if, else if and else, and put the checks in an order that works.
Free, no account needed.