Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
For the integers 1 through 5, i tells you which integer the loop is processing now. It does not remember the work completed before that integer. To retain results, you use separate variables such as sum, product, and evenCount. Each of these is an accumulator: its old value is used to build its next value.
int sum = 0;
int product = 1;
int evenCount = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
product = product * i;
if (i % 2 == 0) {
evenCount = evenCount + 1;
}
}When i is 3, the loop has processed 1, 2, and 3. The value of i is only 3, but sum is 6 because it carries 1 + 2 from earlier iterations and then adds 3. Product carries 1 * 2 and then multiplies by 3. evenCount carries the number of even integers found so far.
After the loop has processed i = 3, which variable retains the total 1 + 2 + 3 for the next iteration?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The first value must match the operation. A sum starts at 0 because adding 0 leaves the first integer unchanged. A product starts at 1 because multiplying by 1 leaves the first integer unchanged. An even count starts at 0 because no integers have been checked before the loop begins.
int sum = 0; // 0 + 1 becomes 1
int product = 1; // 1 * 1 becomes 1
int evenCount = 0; // no even integers seen yetIf product starts at 0, the first update makes it 0, and every later multiplication keeps it at 0. The loop would still visit 1 through 5, but the retained result would no longer be their product. Starting at 1 avoids a special first iteration, because product = product * i works correctly for i = 1 and for every integer after it.
The condition i <= 5 includes 5, so the loop processes five integers: 1, 2, 3, 4, and 5. Replacing it with i < 5 stops when i becomes 5, before the body runs for that value. The loop then processes only 1 through 4.
| CONDITION | PROCESSED INTEGERS | SUM | PRODUCT | EVENCOUNT |
|---|---|---|---|---|
| i <= 5 | 1, 2, 3, 4, 5 | 15 | 120 | 2 |
| i < 5 | 1, 2, 3, 4 | 10 | 24 | 2 |
The sum loses 5, changing from 15 to 10. The product loses a multiplication by 5, changing from 120 to 24. The even count does not change, because 5 is odd, so processing it does not increase evenCount.
Fix the condition so that 5 contributes to the loop.
for (int i = 1; i < 5; i++)Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After each iteration, ask three precise questions: what should be added to sum, what should be multiplied into product, and should evenCount increase because the current integer is even? For i = 1, the count stays 0. For i = 2, it becomes 1. For i = 3, it stays 1. For i = 4, it becomes 2. For i = 5, it stays 2.
| I | SUM UPDATE | PRODUCT UPDATE | EVENCOUNT UPDATE | STATE AFTER ITERATION |
|---|---|---|---|---|
| 1 | 0 + 1 = 1 | 1 * 1 = 1 | 1 is odd, stays 0 | sum = 1, product = 1, evenCount = 0 |
| 2 | 1 + 2 = 3 | 1 * 2 = 2 | 2 is even, becomes 1 | sum = 3, product = 2, evenCount = 1 |
| 3 | 3 + 3 = 6 | 2 * 3 = 6 | 3 is odd, stays 1 | sum = 6, product = 6, evenCount = 1 |
| 4 | 6 + 4 = 10 | 6 * 4 = 24 | 4 is even, becomes 2 | sum = 10, product = 24, evenCount = 2 |
| 5 | 10 + 5 = 15 | 24 * 5 = 120 | 5 is odd, stays 2 | sum = 15, product = 120, evenCount = 2 |
The final state is sum = 15, product = 120, and evenCount = 2. The dry run makes the role of each variable visible: i supplies one current value, sum and product carry old results into new results, and evenCount changes only when the Boolean expression i % 2 == 0 is true.