DSA SheetLesson · no judge

DSA FUNDAMENTALSCONTROL FLOW

Loops Practice 1: The Loop Controls the Values

Reading · 7 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 19

A loop does not run five times just because n is 5

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.

CPPThe C++ loop starts at 1, includes values through n, and increases i after each addition.
int n = 5;
int i = 1;
int sum = 0;

for (i = 1; i <= n; i++) {
    sum = sum + i;
}

// sum is 15
JAVAThe Java loop uses the same three controls and produces the same sum.
int n = 5;
int i = 1;
int sum = 0;

for (i = 1; i <= n; i++) {
    sum = sum + i;
}

// sum is 15

Here, 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.

CHECKPOINT 1Not answered

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 condition is checked before every addition

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 CHECKI <= 5RESULT
1trueAdd 1
2trueAdd 2
3trueAdd 3
4trueAdd 4
5trueAdd 5
6falseExit before adding 6
Each condition check decides whether the current i reaches the addition.
the control flow and changing state of the n = 5 summation loopA flow diagram starts with n = 5, i = 1, and sum = 0. The condition i <= 5 is checked before the addition. For i values 1, 2, 3, 4, and 5, the condition is true, sum = sum + i produces sums 1, 3, 6, 10, and 15, and i++ returns control to the condition. At i = 6, the condition is false, so the loop exits without adding 6. The final sum is 15.start staten = 5, i = 1, sum = 0condition gatei <= 5 ?addition bodysum = sum + isums: 1 → 3 → 6 → 10 → 15counter updatei++exitsum = 15true: i = 1, 2, 3, 4, 5back edgefalse: i = 6Five additions succeed; the sixth check fails before another addition.
The sixth check stops the loop before a sixth addition.

The counter and the sum record different changes

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.

STAGEI BEFORE BODYSUM AFTER BODYI AFTER UPDATE
Start10-
After adding 1112
After adding 2233
After adding 3364
After adding 44105
After adding 55156
The loop state after each successful addition and update.

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.

CHECKPOINT 2Not answered

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 bound i <= n is what includes 5

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.

CPPChanging <= to < excludes the value equal to n.
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 10

The 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.

Previous · if-else and else-if ladder PracticeNext part · Quiz