Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 07 / 34

Control flow & loops

01

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.”

Choose a branch or a repetition rule
ConstructWhen it helpsStopping or selection rule
if / elseChoose between conditionsRun the first matching branch
switchCompare one value against named casesUse break to avoid unintended fall-through
forRepeat with an explicit counterCheck before each pass; update after the body
whileRepeat while a condition holdsThe body must make progress toward false
do / whileRun at least onceCheck after the body
for...ofVisit values in an iterable such as an arrayStop when there are no more values
break / continueLeave a loop or skip to its next passDo 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.

02

Mental model

FOLLOW THE VALUE

count = 1
condition true
add 1

Initialization happens once.

01 / 03Trace the transformation

03

Live example

The loop visits 1, 2, and 3. Its fourth condition check fails, so the branch after the loop can run.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Include the final iteration

The total must include 1, 2, and 3. Fix the loop boundary without changing the logged result directly.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Complete
6

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which array loop directly yields values?
    02What must be true of a terminating while loop?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson