Control flow & loops
Understand the idea
A loading screen never responds because a while loop keeps checking the same value. Nothing inside the loop changes its condition.
A route through the program
A checkout may need a different message for an empty cart. A list may need the same check for every item. Branches and loops let one program handle those situations without writing a separate program for each possible input.
Choosing a branch
Use if and else when different conditions need different work. Conditions use truthiness, so an empty array is still . A switch is useful when one value has several explicit cases; ordinary cases need break unless you intentionally want execution to continue into the next case.
An if checks its condition before entering the block. Add else for the alternative, or else if for another question. A condition is converted to a boolean: empty strings, zero, , and undefined are falsy, while an empty array is truthy. Use an explicit check such as items.length === 0 when you mean “no items.”
| Construct | When it helps | Stopping or selection rule |
|---|---|---|
| if / else | Choose between conditions | Run the first matching branch |
| switch | Compare one value against named cases | Use break to avoid unintended fall-through |
| for | Repeat with an explicit counter | Check before each pass; update after the body |
| while | Repeat while a condition holds | The body must make progress toward false |
| do / while | Run at least once | Check after the body |
| for...of | Visit values in an iterable such as an array | Stop when there are no more values |
| break / continue | Leave a loop or skip to its next pass | Do not skip an essential update in a while loop |
Choosing a loop
A for loop keeps initialization, condition, and update together. A while loop is useful when the number of repetitions is unknown, but you must still explain what changes before its next condition check. Trace the first iteration and the last intended iteration by hand.
Make progress toward stopping
Use for...of to read values from an such as an array. for...in reads enumerable names, including inherited ones, so it is usually the wrong choice for array values. break leaves the loop. continue skips to its next iteration; be careful that it does not skip the update a while loop needs.
Walking through the result
Loops run synchronously unless you deliberately yield through work. A long or endless loop blocks rendering and input. A temporary iteration limit can make a suspect loop inspectable, but it is a debugging aid, not a replacement for a correct termination rule.
For the live example, trace one repetition at a time: count is 1 and total becomes 1; count is 2 and total becomes 3; count is 3 and total becomes 6. Next, count reaches 4, the condition is false, and the loop ends. You can do this on paper before asking the computer. It makes an off-by-one error much easier to spot.
Mental model
FOLLOW THE VALUE
Initialization happens once.
01 / 03Trace the transformation
Live example
The loop visits 1, 2, and 3. Its fourth condition check fails, so the branch after the loop can run.
Your console output appears here.
Guided challenge
Include the final iteration
The total must include 1, 2, and 3. Fix the loop boundary without changing the logged result directly.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Complete 6
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.