Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A continue statement stops the current iteration at the point where it runs. Control then moves to the next iteration, not out of the loop. For this loop, the counter reaches 3, the if statement runs continue, and the print statement is skipped only for that counter.
for (int counter = 1; counter <= 8; counter++) {
if (counter == 3) {
continue;
}
cout << counter << " ";
}In a for loop, continue still allows the loop update to run. After continue skips the rest of the body at counter 3, counter++ changes the counter to 4. The loop condition is checked again, so the loop continues with 4, then prints 4 through 8. Its output is 1 2 4 5 6 7 8.
What does this loop print when it contains continue only for counter 3?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A break statement exits the loop immediately. Add a second if statement for counter 7 after the continue test. Counters 1 and 2 print, counter 3 skips its print and moves to the next iteration, and counters 4, 5 and 6 print. When the counter reaches 7, break exits the loop before either 7 or 8 can be printed.
for (int counter = 1; counter <= 8; counter++) {
if (counter == 3) {
continue;
}
if (counter == 7) {
break;
}
cout << counter << " ";
}The two keywords are not interchangeable ways to skip a value. At 3, continue skips one iteration and lets the loop reach 4. At 7, break ends the entire loop, so the counter never reaches 8. The resulting output is 1 2 4 5 6.
Execution visits the first test before the second test, and it reaches the print statement only when both tests are false. For counter 3, the first test is true, so continue jumps away before the second test and print statement. For counter 7, the first test is false, the second test is true, and break jumps away before the print statement.
| COUNTER | FIRST TEST: COUNTER == 3 | SECOND TEST: COUNTER == 7 | RESULT |
|---|---|---|---|
| 1 | false | false | Print 1, then update |
| 2 | false | false | Print 2, then update |
| 3 | true | Not reached | Continue, then update |
| 4 | false | false | Print 4, then update |
| 5 | false | false | Print 5, then update |
| 6 | false | false | Print 6, then update |
| 7 | false | true | Break, then exit |
| 8 | Not reached | Not reached | Not visited |
A jump cannot undo a statement that already ran. If you move the print statement above both tests, counter 3 and counter 7 are printed before their jumps take effect, so the output becomes 1 2 3 4 5 6 7. To keep the output as 1 2 4 5 6, the print statement must come after both jump tests.
for (int counter = 1; counter <= 8; counter++) {
if (counter == 3) {
continue;
}
if (counter == 7) {
break;
}
cout << counter << " ";
}Fill the two keywords so counter 3 is skipped, counter 7 ends the loop, and the print statement produces 1 2 4 5 6.
for (int counter = 1; counter <= 8; counter++) {
if (counter == 3) {
______;
}
if (counter == 7) {
______;
}
cout << counter << " ";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A while loop does not place its counter update in a header. If the update is below continue, reaching counter 3 skips that update along with every other later statement in the body. The loop condition is checked again with counter still equal to 3, so continue runs again forever.
int counter = 1;
while (counter <= 8) {
if (counter == 3) {
continue;
}
if (counter == 7) {
break;
}
cout << counter << " ";
counter++;
}To fix this version, update the counter before continue can skip it. At counter 3, counter++ changes the value to 4, then continue starts the next iteration. The same idea preserves the intended output: the counter advances after printing ordinary values, and break at 7 exits before 7 is printed.
int counter = 1;
while (counter <= 8) {
if (counter == 3) {
counter++;
continue;
}
if (counter == 7) {
break;
}
cout << counter << " ";
counter++;
}