Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
This loop prints the numbers 1 through 5:
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}The header has three expressions separated by semicolons. int i = 1 initializes the counter, so the first value of i is 1. i <= 5 is the condition that decides whether the body may run. i++ is the update that advances the counter after the body finishes.
The body prints the current value of i. It does not print a value created by the update. On the first body execution, i is still 1 because the update has not run yet.
Which expression in for (int i = 1; i <= 5; i++) advances the counter?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The loop follows one fixed order. First, int i = 1 runs once. The condition is then tested. If it is true, the body prints the current value of i. Only after the body finishes does i++ run. The loop then returns to the condition, not to the initialization expression.
| STEP | VALUE OF I | ACTION |
|---|---|---|
| 1 | not yet set | Run int i = 1 |
| 2 | 1 | Test i <= 5, then print 1 |
| 3 | 2 | Run i++ |
| 4 | 2 | Test i <= 5, then print 2 |
| 5 | 3 | Run i++ |
| 6 | 3 | Test i <= 5, then print 3 |
| 7 | 4 | Run i++ |
| 8 | 4 | Test i <= 5, then print 4 |
| 9 | 5 | Run i++ |
| 10 | 5 | Test i <= 5, then print 5 |
| 11 | 6 | Run i++ |
| 12 | 6 | Test i <= 5, which is false, then stop |
The same pattern continues after each printed value. After printing 5, the update changes i from 5 to 6. The next condition test fails, so the body does not run with i equal to 6. The update runs five times in total, once after each body execution, while the initialization runs only once.
With i equal to 5, the condition i <= 5 is true because equality is allowed. The body therefore prints 5. The update then changes i to 6, and the next test asks whether 6 <= 5. That test is false, so the loop ends before printing 6.
If you change only the condition to i < 5, the loop still starts at 1 and still updates after each body execution. However, when i is 5, the test 5 < 5 is false. The body is skipped at that point, so the output becomes 1 2 3 4 instead of 1 2 3 4 5.
Replace i < 5 so this loop prints 1 2 3 4 5. Type only the corrected condition.
for (int i = 1; i < 5; i++) {
cout << i << " ";
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
This loop has a known starting value, a known boundary, and a known update: start at 1, continue through 5, and increase by 1 after each print. Putting those three control steps in the header makes the counter's journey easy to read in one place.
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}The while form performs the same actions in the same order. The for form gathers the initialization expression, condition expression, and update expression into its header, but it does not change how repetition works. In both forms, the body first prints 1, and the update happens only after that first print.