Loops: formulas, shortcuts & solved examples
What it is
A loop repeats a block of statements while a condition holds. Placement pseudocode rounds (the style used by many Indian IT recruiters) love loops because one small change in a bound, a step or an update order completely changes the output. Your job is to trace the variables accurately and fast.
Key formulas and rules
Counting iterations
for i = a to b(step 1, inclusive): runs b − a + 1 times, or zero times if a > b.for i = a to b step k: runs floor((b − a) / k) + 1 times when a ≤ b.while (i < n)withi = i * 2starting at 1: runs ceil(log2 n) times.- Nested loop where the inner runs from
itonand the outer from 1 to n: total n(n + 1) / 2. - Inner from 1 to
i − 1: total n(n − 1) / 2.
Useful sums
- 1 + 2 + ... + n = n(n + 1) / 2
- Sum of multiples of k up to n = k × m(m + 1) / 2, where m = floor(n / k)
- 1² + 2² + ... + n² = n(n + 1)(2n + 1) / 6
Tracing rules
- Check whether the condition is tested before (while/for) or after (do-while) the body. A do-while always runs at least once.
- Note the order of statements inside the body: printing before or after the update changes the output.
- Watch for integer division: in most pseudocode,
7 / 2is 3 when both are integers. - After the loop ends, the loop variable holds the first value that failed the condition.
Shortcuts
- Digit loops.
while (n > 0) { d = n mod 10; n = n / 10 }processes digits right to left. If the body buildsr = r * 10 + d, the result is the reversed number. Recognise the pattern and skip the trace. - Skip-and-sum. If a loop adds
ionly wheni mod k == 0, use the multiples formula instead of tracing every value. - Table for nested loops. Write a small table with columns i, j range and count. For n up to 5, a table is faster and safer than a formula you half remember.
Solved examples
Example 1: Conditional sum
Integer s = 0
for (i = 1 to 20)
if (i mod 4 == 0)
s = s + i
end for
Print s
Solution. Multiples of 4 up to 20 are 4, 8, 12, 16, 20. Here m = floor(20 / 4) = 5, so s = 4 × 5 × 6 / 2 = 60.
Example 2: While loop with step 2
Integer n = 5, f = 1
while (n > 1)
f = f * n
n = n - 2
end while
Print f
Solution. Iteration 1: f = 5, n = 3. Iteration 2: f = 15, n = 1. The condition 1 > 1 is false, so the loop stops. Output 15 (this is the "odd double factorial" 5 × 3).
Example 3: Counting a nested loop
Integer c = 0
for (i = 1 to 4)
for (j = i to 4)
c = c + 1
Print c
Solution. For i = 1 the inner loop runs 4 times, then 3, 2, 1. Total 4 + 3 + 2 + 1 = 10, which matches n(n + 1) / 2 with n = 4. Output 10.
Example 4: Reversing a number
Integer n = 4729, r = 0
while (n > 0)
r = r * 10 + (n mod 10)
n = n / 10
end while
Print r
Solution. Trace: r = 9, n = 472; r = 92, n = 47; r = 927, n = 4; r = 9274, n = 0. Output 9274. If the input had a trailing zero, such as 1200, the result would be 21, because leading zeros vanish.
Common mistakes
- Off-by-one on inclusive bounds.
for i = 1 to 10in pseudocode usually includes 10, unlike Python'srange(1, 10). Read the question's convention carefully. - Forgetting the final value of the loop variable. After
for i = 1 to 5, some questions printi; in C-style loops it is 6, not 5. - Ignoring integer division.
n = n / 10truncates. Treating it as a decimal gives wrong digit counts. - Treating do-while like while. If the condition is false at the start, a do-while still executes once and may print something.
- Updating in the wrong order. In
print i; i = i + 2versusi = i + 2; print i, the first value printed differs. Trace statements in the exact order written.
Practice now: https://www.testsworld.com/mocks