All lessonsOpen Open Open Open Open
4. Loops
do-while, break, continue
0 of 5 activities0%
Reading 1
Extra loop tools
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
In a for loop, what does continue do?
Fill in 3
Exit early
Try it 4
Skip threes
Notice 3 is skipped. Try skipping 2 and 4 instead.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
First multiple of 7
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…