Iteration CS Thinking Example 4

Follow the full solution, then compare it with the other examples linked below.

Example 4

easy
Trace this loop: SET total = 2. REPEAT 4 TIMES: SET total = total + 3. OUTPUT total.

Solution

  1. 1
    Step 1: Start with total = 2. After each repetition the totals are 5, 8, 11, and 14.
  2. 2
    Step 2: After 4 repetitions, OUTPUT total gives 14.

Answer

14
Iteration repeats the same instructions multiple times. Tracing the variable after each repetition is the safest way to understand what a loop does.

About Iteration

Repeating a block of instructions multiple times until a stopping condition is satisfied. Iteration is one of the three fundamental control structures (along with sequence and selection) and is implemented through while loops, for loops, and other looping constructs.

Learn more about Iteration โ†’

More Iteration Examples