Python Flow of Control: if, for, while and range() (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
Python marks a block with a colon and equal indentation. if-elif-else runs the first block whose condition is true; for variable in sequence repeats for each item, with range(start, stop, step) giving integers from start up to but not including stop; while repeats as long as its condition is true; break leaves a loop and continue skips to the next iteration. Standard programs include sum, factorial, digit sum, prime test and the Fibonacci series.
The while loop repeats its indented body while the condition is Yes; range(2, 10, 3) steps by 3 and stops before the hollow 10.
Illustration drawn for QuestionBuddy.
Understand Python Flow of Control: if-elif-else, for, range() and while
Indentation. A block is a group of statements with the same indentation (usually 4 spaces) after a line that ends with a colon. Wrong indentation causes IndentationError.
Conditional flow.
marks = 72
if marks >= 90:
grade = 'A'
elif marks >= 70:
grade = 'B'
else:
grade = 'C'
print(grade) # Brange(). range(n) gives 0 to n - 1; range(a, b) gives a to b - 1; range(a, b, s) steps by s (negative s counts down). range(5, 0, -2) gives 5, 3, 1; range(2, 10, 3) gives 2, 5, 8.
for loop.
for i in range(1, 6):
print(i, end=' ') # 1 2 3 4 5for can also iterate over a string or list: for ch in 'Hi'.
while loop. Needs a condition that eventually becomes false.
n = 4271; t = 0
while n > 0:
t += n % 10
n //= 10
print(t) # 14break and continue; else on loops. break exits the loop; continue skips to the next iteration. The else block of a loop runs only if the loop finished without break.
Standard programs. Sum 1 to 10 = 55; factorial of 5 = 120; Fibonacci: 0 1 1 2 3 5 8; prime test: n has no divisor from 2 to n - 1 (or to the square root).
Python Flow of Control: if-elif-else, for, range() and while: key facts
- range()
range stop range start, stop range start, stop, step stop is excluded - Integer sequences.
- Digit extraction
- last digit = n % 10 | remaining number = n // 10
- Used in while loops.
- Loop count
- Iterations of range(a, b, s) = ceil((b - a) / s) | for positive s and b > a
- Count the values.
How to answer Python Flow of Control: if-elif-else, for, range() and while questions
Trace the loop with a table of variable values.
- 1Write the variables and their starting values.
- 2For each iteration record the new values and any output.
- 3Check the stop condition (stop value excluded in range).
- 4Write the final output exactly, with sep and end.
Python Flow of Control: if-elif-else, for, range() and while in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Range with step
Suppose a teacher wants to print the roll numbers of every third student from roll 2 to roll 9.
Which range() call is used and what is printed?
Try it first, then show the solutionSolution
- The sequence starts at 2, adds 3 each time and stops before 10: range(2, 10, 3).
- Values: 2, 5, 8.
Answer: range(2, 10, 3) gives 2, 5, 8.
Memory hook: Stop is never included.
Tricks to remember Python Flow of Control: if-elif-else, for, range() and while
Short memory aids. Say each one aloud once, then use it on a question the same day.
Colon and indent
Colon opens a block; indentation defines it.
Use it when: Syntax.
Stop is excluded
range(1, 6) ends at 5.
Use it when: range().
Loop else means no break
else runs if the loop was not broken.
Use it when: else clause.
Python Flow of Control: if-elif-else, for, range() and while: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: range · 1 mark · MCQBoard level
The output of: for i in range(5, 0, -2): print(i, end=' ') is:
(a) 5 4 3 2 1 (b) 5 3 1 (c) 5 3 (d) 0 2 4
Try it first, then show the solutionSolution
- Start 5, step -2, stop before 0: 5, 3, 1.
Answer: (b) 5 3 1
Example 2: Grade program · 4 marks · Short answerBoard level
Write a program to read marks and print the grade: A for 90 and above, B for 70 to 89, C for 50 to 69, otherwise D. Give the output for 72, 90 and 49. (4 marks)
Try it first, then show the solutionSolution
- Use if-elif-else in descending order.
- 72 gives B; 90 gives A (the first condition is true); 49 gives D.
Answer:
Outputs: B, A, D.m = int(input())
if m >= 90:
print("A")
elif m >= 70:
print("B")
elif m >= 50:
print("C")
else:
print("D")
Example 3: Trace the loops · 6 marks · Short answerHarder board level
Give the output: (a) for i in range(2, 10, 3): print(i, end=' ') (b) s = 0; for i in range(1, 11): s += i; print(s) (c) n = 4271; t = 0; while n > 0: t += n % 10; n //= 10; print(t) (d) a, b = 0, 1; for i in range(7): print(a, end=' '); a, b = b, a + b. (6 marks)
Try it first, then show the solutionSolution
- (a) 2 5 8.
- (b) 1 + 2 + ... + 10 = 55.
- (c) digits 1, 7, 2, 4 sum to 14.
- (d) Fibonacci: 0 1 1 2 3 5 8.
Answer: (a) 2 5 8 (b) 55 (c) 14 (d) 0 1 1 2 3 5 8
Example 4: Prime test with break and else · 5 marks · Long answerHarder board level
Write a program to test whether n is prime using a loop with break and the loop else, and trace n = 7 and n = 9. (5 marks)
Try it first, then show the solutionSolution
- Check divisors from 2 to n - 1; if one divides n, print "not prime" and break; else print "prime".
- n = 7: no divisor among 2 to 6, so the else block runs: prime.
- n = 9: 9 % 3 == 0 at i = 3, so "not prime" and break (the else block is skipped).
Answer:
n = 7: prime; n = 9: not prime. (For n = 1 or 2 the loop does nothing and "prime" is printed, so handle n < 2 separately.)n = int(input())
for i in range(2, n):
if n % i == 0:
print("not prime")
break
else:
print("prime")
Example 5: continue and nested loops · 5 marks · Long answerHOTS / competency
Give the output: (a) for i in range(1, 8): if i % 3 == 0: continue; print(i, end=' ') (b) for i in range(1, 4): for j in range(1, i + 1): print(j, end=' '); print() and say how many times the inner print executes in (b). (5 marks)
Try it first, then show the solutionSolution
- (a) continue skips 3 and 6: 1 2 4 5 7.
- (b) i = 1: 1; i = 2: 1 2; i = 3: 1 2 3, each followed by a new line.
- The inner print executes 1 + 2 + 3 = 6 times.
Answer: (a) 1 2 4 5 7
(b) 1
1 2
1 2 3 (six values printed by the inner print).
Where students lose marks in Python Flow of Control: if-elif-else, for, range() and while
The slips examiners see most often, and what to write instead.
Mistake: Including the stop value in range
Inclusive thinking.
Wrong working
range(1, 10) includes 10
Right working
ends at 9
Do this: Stop excluded.
Usually costs: 1-2 marks.
Mistake: Wrong indentation
Mixed levels.
Wrong working
print inside the wrong block
Right working
Align with the block it belongs to
Do this: 4 spaces per level.
Usually costs: 1-2 marks.
Mistake: Infinite while loop
Forgot to update.
Wrong working
No n //= 10
Right working
Update the variable
Do this: Check the loop variable changes.
Usually costs: 2 marks.
Exam tips
- Make a trace table for loops.
- Remember the colon after if, elif, else, for and while.
- Use end=' ' to keep output on one line.
Practice questions on Python Flow of Control: if-elif-else, for, range() and while
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
The statement that skips the rest of the current iteration and goes to the next is:
(a) break (b) continue (c) pass (d) exitHint
Not break.
Answer
(b) continue
Q2 · 3 marks · Short answerBoard level
Write a program to print the multiplication table of 7 up to 7 x 5 and give the output. (3 marks)
Hint
range(1, 6).
Answer
for i in range(1, 6): print(7, "x", i, "=", 7 * i). Output: 7 x 1 = 7, 7 x 2 = 14, 7 x 3 = 21, 7 x 4 = 28, 7 x 5 = 35 (each on its own line).
Q3 · 3 marks · Short answerHarder board level
Give the output: s = 0; for i in range(1, 20, 4): s += i; print(i, s) and count the iterations. (3 marks)
Hint
1, 5, 9, 13, 17.
Answer
i takes 1, 5, 9, 13, 17 (5 iterations); s = 45; after the loop i = 17: output 17 45.
Q4 · 5 marks · Long answerHarder board level
Write a program to input a number and print the factorial using a loop, and the number of digits using a while loop. Give the output for 5 and 4271. (5 marks)
Hint
120 and 4 digits.
Answer
f = 1; for i in range(1, n + 1): f *= i gives 120 for n = 5. For digits: c = 0; while n > 0: n //= 10; c += 1 gives 4 for 4271.
Q5 · 3 marks · Short answerHOTS / competency
A student writes: i = 1\nwhile i <= 3:\n print(i)\nand the program never stops. Explain why, correct it, and give the output of the corrected loop. (3 marks)
Hint
No update.
Answer
The variable i is never changed, so the condition stays true. Add i += 1 inside the loop. Output: 1, 2, 3 on separate lines.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.range(1, 6) gives?Show answer
1 to 5.
2.What ends an if line?Show answer
A colon.
3.break does?Show answer
Exits the loop.
4.Loop else runs when?Show answer
The loop ends without break.
5.Digit extraction uses?Show answer
% 10 and // 10.
Python Flow of Control: if-elif-else, for, range() and while: frequently asked questions
Is indentation optional?
No, it defines blocks.
Can range use floats?
No, integers only.
What does end do in print?
Sets what follows the output.
What is pass?
A do-nothing statement.