All lessons

4. Loops

do-while, break, continue

0 of 5 activities0%

Reading 1

Extra loop tools

Open

do { body } while (condition); runs the body first, then checks. Use it when the body should run at least once.

break leaves the nearest loop immediately. continue skips the rest of the current iteration and goes to the next check/step.

Prefer clear loop conditions when you can; use break and continue when they make the intent clearer.

int x;
do {
    cin >> x;
} while (x < 0);

for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    cout << i << endl;
}

Check 2

continue effect

Open

In a for loop, what does continue do?

Fill in 3

Exit early

Open

The keyword that exits a loop immediately is

Try it 4

Skip threes

Open

Notice 3 is skipped. Try skipping 2 and 4 instead.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

First multiple of 7

Open

Read integers until you find one that is a multiple of 7 (x % 7 == 0 and x != 0). Print that number and stop. There will always be one.

main.cpp
Loading editor…