DSA FUNDAMENTALS › CONTROL FLOW
In the running loop, n stores the value 5, but n alone does not decide how many times the loop body runs. The initialization sets i to 1, the condition decides whether the body may run, and the update changes i after each pass. These three controls decide which values reach the addition. The loop starts with sum equal to 0 and adds the current value of i each time.
int n = 5;
int i = 1;
int sum = 0;
for (i = 1; i <= n; i++) {
sum = sum + i;
}
// sum is 15int n = 5;
int i = 1;
int sum = 0;
for (i = 1; i <= n; i++) {
sum = sum + i;
}
// sum is 15Here, i = 1 is the initialization value, i <= n is the condition, and i++ is the update. Since n is 5, the condition allows i values 1, 2, 3, 4, and 5 into the body. Changing any of these controls changes the values that are added, even though n remains 5.
Which part of the running loop allows i = 5 to enter the loop body?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The loop does not add first and check later. It checks i <= 5 before every execution of the body. The checks for i = 1 through i = 5 are true, so those values are added. After the update following i = 5, i becomes 6. The next check, 6 <= 5, is false, so the body is skipped and the loop exits before 6 can be added.
| I AT CHECK | I <= 5 | RESULT |
|---|---|---|
| 1 | true | Add 1 |
| 2 | true | Add 2 |
| 3 | true | Add 3 |
| 4 | true | Add 4 |
| 5 | true | Add 5 |
| 6 | false | Exit before adding 6 |
The counter i chooses the value that the next pass will add. The sum records everything added so far. During the pass where i is 5, the loop body changes sum from 10 to 15. The update then changes i from 5 to 6. These changes serve different jobs: i moves the loop forward, while sum preserves the accumulated result.
| STAGE | I BEFORE BODY | SUM AFTER BODY | I AFTER UPDATE |
|---|---|---|---|
| Start | 1 | 0 | - |
| After adding 1 | 1 | 1 | 2 |
| After adding 2 | 2 | 3 | 3 |
| After adding 3 | 3 | 6 | 4 |
| After adding 4 | 4 | 10 | 5 |
| After adding 5 | 5 | 15 | 6 |
The final successful body execution has i = 5 and produces sum = 15. The update still runs after that body execution, so the next state is i = 6 and sum = 15. The next condition check uses i = 6, not i = 5, and rejects the loop.
After the pass that adds 5, what are the final values of sum and the next value of i?
sum = sum + i; // i is 5 here
// then i++
// final state: sum = ?, i = ?Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The condition i <= n includes the endpoint because it permits i to equal n. With n = 5, the body runs for i = 1, 2, 3, 4, and 5, producing sum = 15. If you replace the condition with i < n, the body runs only while i is 1, 2, 3, or 4. When i becomes 5, 5 < 5 is false, so 5 is skipped and the sum stops at 10.
int n = 5;
int sum = 0;
for (int i = 1; i < n; i++) {
sum = sum + i;
}
// i values added: 1, 2, 3, 4
// sum is 10The update is not what decides whether 5 is included. The update only moves the counter from one value to the next. The condition makes the inclusion decision: i <= n accepts the value 5, while i < n rejects it. A one-character change therefore changes both the number of body executions and the final accumulated sum.