- Recognise the three kinds of loops and read their flowcharts
- Run a loop step by step with a trace table: sum, product, count, maximum, digits
- Find the number of iterations with an inequality and trace two consecutive loops
- Find initial values and the largest inputs from the output
A teacher has to add up the scores of 30 students. Instead of writing “add the next score to the sum” 30 times, it is enough to write it once and say “repeat 30 times”. An algorithm with repeated steps is a loop algorithm. Loops were the most frequent topic of the 2025–2026 entrance exams: Python tasks labelled “Loop statement”, plus flowchart tasks on the number of iterations, two consecutive loops and reverse questions such as “which input must be given so that…”. In this lesson you will learn to read loops in flowcharts and to run them without mistakes using a trace table.
Loops and their kinds
An algorithm in which a group of commands is repeated several times is a loop algorithm. The repeated commands form the loop body, and one execution of the body is an iteration (one pass of the loop).
- Counter loop — the number of repetitions is known in advance: the loop variable runs from a start value to an end value with a fixed step. In Python:
for i in range(1, 11). - Loop with the condition first — the condition is checked before the body, and the body is repeated while the condition is true. If the condition is false from the start, the body runs zero times. In Python:
while. - Loop with the condition last — the body runs first and then the condition is checked, so the body runs at least once. Python has no separate statement for it; it is written with
while True:andbreak.
for i from 1 to 5
S = S + i
end for
while a < b
a = a + 5
end while
repeat
x = x − 3
while x > 0In DİM flowcharts a counter loop is also drawn with a diamond: first i = 1, then the diamond i ≤ n, and the body must contain i = i + 1. As soon as you see an arrow going back up, you know there is a loop; look at which exit of the diamond goes back — that is the condition for continuing the loop.
Trace tables and typical loop tasks
In a loop’s trace table each row is one check of the condition: we write the answer to the condition, then the new values of the variables after the body. In the last row the condition is “No” and the loop ends. Note: the condition is always checked one time more than the number of iterations.
- Ssum; starts at 0
- Pproduct; starts at 1 (with 0 it would stay 0 forever)
- kcount (counter); starts at 0
The three “collector” variables of a loop and their initial values
S = 0; i = 1; while i ≤ 5: S = S + i·i; i = i + 2. S is output. What is printed?
Show solutionHide solution
| Check | i (before) | i ≤ 5 | S = S + i·i | i = i + 2 |
|---|---|---|---|---|
| 1 | 1 | Yes | 0 + 1 = 1 | 3 |
| 2 | 3 | Yes | 1 + 9 = 10 | 5 |
| 3 | 5 | Yes | 10 + 25 = 35 | 7 |
| 4 | 7 | No |
1) P = 1; for i from 1 to 5: P = P · i. What is output?
2) In the same algorithm P = 0 was written by mistake. What is printed now?
Show solutionHide solution
2) 0 · i is always 0: P stays 0 at every step, the output is 0. A product must start at 1, a sum at 0.
- n % 10the last digit of the number (remainder when divided by 10)
- n // 10the number without its last digit (whole division)
Loop over digits: while n > 0, take the last digit and drop it
n = 4072; s = 0; k = 0. While n > 0: s = s + n % 10; k = k + 1; n = n // 10. s and k are output.
Show solutionHide solution
407 > 0: s = 9, k = 2, n = 40
40 > 0: s = 9 (digit 0), k = 3, n = 4
4 > 0: s = 13, k = 4, n = 0
0 > 0 — No. Output: 13 4. A zero digit does not change the sum but is counted.
n = 4072
s = 0
k = 0
while n > 0:
s = s + n % 10
k = k + 1
n = n // 10
print(s, k)
a = [12, 7, 25, 3, 18]
m = a[0]
for x in a:
if x > m:
m = x
print(m)▸ Expected output
13 4 25
while loop over digits and a search for the maximum: the candidate m is the first element, then every element is compared with it. Exam programs read n with n = int(input()).DİM tasks: the number of iterations and two consecutive loops
In DİM loop tasks the loop often repeats dozens of times — writing every step is long and risky. So you need the pattern: if a variable changes by the same amount on every pass, its value after k passes can be written with a formula. Then the stopping condition of the loop becomes an inequality.
- a₀, b₀initial values of the variables (a₀ < b₀)
- p, qthe increase of a and the decrease of b on each pass
- knumber of passes of the loop “a < b”
- ⌈ ⌉rounding up: 10.875 → 11
The loop stops the first time a ≥ b: the gap shrinks by p + q on every pass
- 1Find the variables
Which variables are in the loop condition, and by how much do they change on each pass of the body?
- 2Write them after k passes
For example, a = a₀ + p·k, b = b₀ − q·k; if the change is not constant, write the first 4–5 passes in a table and look for the pattern.
- 3Solve the stopping condition
Write the opposite of the loop condition (for example, a ≥ b) as an inequality and find the smallest natural k that satisfies it.
- 4Check the boundary
Compute the values for k − 1 and k: after k − 1 passes the condition must still be true, after k passes it must be false.
For a = 3 and b = 90 the algorithm is: while a < b: a = a + 5; b = b − 3. Find the number of passes of the loop.
Show solutionHide solution
The loop stops when 3 + 5k ≥ 90 − 3k → 8k ≥ 87 → k ≥ 10.875 → the smallest whole k = 11.
Check: k = 10: a = 53, b = 60, 53 < 60 — the loop goes on; k = 11: a = 58, b = 57 — it stops ✓.
Using the flowchart above, find the value of a output after the algorithm runs.
Show solutionHide solution
Loop II starts with a = 3, b = 8 (b > 4):
b = 8 − 3 = 5, a = 3 + 5 = 8
b = 5 − 3 = 2, a = 8 + 2 = 10
2 > 4 — No. Output: 10.
Note: in loop II b decreases first, and then the new value of b is added to a.
Algorithm: S = 0; b = 25; a = ?; while a < b: a = a + 5; S = S + a; b = b − 2. S is output. The loop stopped when a = 23, b = 19. Find the initial value of a and then S.
A) 31 B) 54 C) 49 D) 77 E) 36
Show solutionHide solution
a increases by 5 per pass: a₀ = 23 − 3·5 = 8.
Run: 8 < 25 → a = 13, S = 13, b = 23; 13 < 23 → a = 18, S = 31, b = 21; 18 < 21 → a = 23, S = 54, b = 19; 23 < 19 — No.
S = 54, answer B. (“31” is for those who miss one pass.)
Reverse tasks: the input from the outputs
a and b are input; n = 3.
Loop I: while a < 10·n: print “AB”; a = a + n.
Loop II: while b ≥ 20·n: print “BA”; b = b − n.
During execution “AB” was printed 5 times and “BA” 4 times. Find the sum of the largest possible natural values of a and b.
Show solutionHide solution
a + 4·3 < 30 → a < 18; a + 5·3 ≥ 30 → a ≥ 15. So a ∈ {15, 16, 17}, the largest is 17.
Loop II ran 4 times: b − 3·3 ≥ 60 → b ≥ 69; b − 4·3 < 60 → b < 72. So b ∈ {69, 70, 71}, the largest is 71.
Sum: 17 + 71 = 88.
The flowchart is run 7 times; each time one of the numbers 14, 27, 33, 40, 51, 8, 60 is entered for x, in order. How many times in total is 1 printed?
Show solutionHide solution
Check with the digit sum: 27, 33, 51, 60 are divisible; 14, 40, 8 are not.
Answer: 4. There is no need to trace every input step by step — it is enough to understand what the loop does.
k = 0; for n from −50 to 50 (step 1): if (n + 7)·(20 − n) > 0 then k = k + 1. k is output.
A) 27 B) 26 C) 13 D) 28 E) 25
Show solutionHide solution
The product is positive when both factors are positive: n > −7 and n < 20 (both negative is impossible).
Integers: −6, −5, …, 19 → 19 − (−6) + 1 = 26, answer B. (“27” comes from counting one of the ends, −7 or 20.)
Nested loops and infinite loops
A loop body may contain another loop — this is a nested loop. On every pass of the outer loop the inner loop runs completely from start to end. If the inner loop’s bounds do not depend on the outer variable, the total number of passes is a product: 4 · 3 = 12. If they do (for example, j from 1 to i), you add up the passes row by row.
1) s = 0; for i from 1 to 4: for j from 1 to 3: s = s + 1. s = ?
2) s = 0; for i from 1 to 4: for j from 1 to i: s = s + 1. s = ?
Show solutionHide solution
2) i = 1: 1 pass; i = 2: 2; i = 3: 3; i = 4: 4 → s = 1 + 2 + 3 + 4 = 10.
You have learned to read loops; the next step is to build them yourself. The written part of the exam asks for a complete flowchart that reads n numbers and works with a counter loop — that is the topic of the lesson «Building flowcharts: the written tasks». The Python form of the same loops is studied in the programming section.
Check the formula of Example 4: for a = 3, b = 90, in a while a < b: loop increase a by 5, decrease b by 3, count the passes with a counter and print the count.
a = 3
b = 90
k = 0
# loop here
print(k)▸ Expected output
11
Key points
- There are three kinds of loops: counter, condition-first and condition-last; in the last one the body runs at least once.
- In a trace table each row is one check of the condition; the condition is checked once more than the number of passes.
- A sum starts at 0, a product at 1, a counter at 0; digits are split off with n % 10 and n // 10.
- Number of passes: write the values after k passes and solve the stopping condition as an inequality.
- “Ran k times” = the condition was true at the k-th check and false at the (k + 1)-th; these two inequalities give the range of the input.
- In a nested loop the inner loop runs fully on every pass of the outer; with independent bounds the passes multiply.
Check yourself
12 questions. Every correct answer earns XP.