From Zero to Deploy · Module 2 · Lesson 2.3
2.3 Conditions and loops, deciding and repeating
How code makes decisions with if and else, compares values, and repeats tasks with for without you copying anything.
if: “if it’s like this, do that”
Every app makes decisions: if the cafe is open, show the green button; if the password is wrong, show a warning. In code, that’s if. else is what happens when the condition doesn’t hold.
The condition inside the parentheses always gives true or false. These are the comparisons you’ll see most:
| Code | Means |
|---|---|
a === b |
a equals b |
a !== b |
a is different from b |
a > b, a >= b |
greater than, greater than or equal |
a < b, a <= b |
less than, less than or equal |
x && y |
x and y are true |
x || y |
x or y is true (at least one) |
!x |
not x (flips it: true becomes false) |
Challenge: the cafe now also opens at night, from 20 to 23. Change the condition to (hour >= 7 && hour < 18) || (hour >= 20 && hour < 23) and test it with isOpen(21).
else if: more than two paths
When there are several possibilities, you chain else if. The code tests one condition at a time, top to bottom, and stops at the first one that’s true. That’s how the 3D game decides the color of the terrain:
Challenges:
- Add snow on the mountain tops: replace the last
elsewithelse if (height < 60) { return 'rock'; } else { return 'snow'; }and test it withterrain(80). - Now change the order: put the
height < 30test first. What doesterrain(-5)return? Why did the sea disappear?
Order matters: -5 is also less than 30, and since that test comes first, it wins. When a chain of else if gives the wrong result, check the order.
Now it’s your turn. Write the code in the editor and click Run tests: the tests try your function with several numbers, including the ones on the edge, like 5 and 10.
= is not ===
One equals sign (=) puts a value into a variable. Three (===) compare two values. Mixing them up is a classic bug:
You’ll also see == (two signs) in older code. It compares in a looser way that leads to surprises. Always prefer ===.
for: repeating without copying
for repeats a block of code several times. It has three parts, separated by ;: where it starts (let i = 1), how long it keeps going (i <= 5), and what changes on each lap (i++, which adds 1).
Challenge: in the first for, change the 5 to 20 and show only the even numbers. Put an if inside the for: if (i % 2 === 0) { console.log(i); }. % gives the remainder of a division: a number is even when the remainder of dividing it by 2 is zero.
Where you’ll see this in the course
- In the 3D game, a
forgoes through every point of the terrain (thousands of them) and a chain ofif / else ifpicks the color: sea, sand, forest, or rock. It’s the example above, only bigger. - Also in the game, an
ifchecks whether the plane went below the ground or left the map (several conditions joined with||) and, if so, callsrestart(). And whenpassed === rings.length, you’ve flown through every ring and the race is over. - In the AI app, the server starts with
if (!imageBase64 || !ACCEPTED_TYPES.includes(type)): if no photo came in or the type isn’t accepted, it turns the request down before spending anything on the AI.!means “not”.
Mark it complete to track your progress.