Counting loop iterations: Class 11 practice
range(a, b, s) produces a, a + s, … stopping before b. Read the method, follow the three worked examples, then try the six questions.
What you should know first
- range()
The idea behind counting loop iterations
range(a, b, s) produces a, a + s, … stopping before b. Nested loops multiply: an outer loop of m and an inner loop of n run the body m × n times.
Step-by-step method
- Find the values produced by each range.
- Count them: ⌈(b − a)/s⌉ when positive.
- Multiply for nested loops (or add a triangular count when the inner depends on the outer).
Worked example 1 (Basic)
How many times does the body of for i in range(0, 12, 2): run?
- Values 0, 2, … < 12
- Count = 6
- Answer: 6
Worked example 2 (Standard)
for i in range(7): for j in range(6): count += 1. What is count?
- 7 × 6
- Answer: 42
Worked example 3 (Challenge)
for i in range(8): for j in range(i): count += 1. What is count?
- 0 + 1 + … + 7
- = 28
- Answer: 28
Common mistakes and misconceptions
- Including the stop value
- Adding instead of multiplying nested counts
Practice questions
- (Basic) How many times does the body of for i in range(1, 16, 2): run?
- (Basic) How many times does the body of for i in range(0, 11, 1): run?
- (Standard) for i in range(3): for j in range(4): count += 1. What is count?
- (Standard) for i in range(5): for j in range(8): count += 1. What is count?
- (Challenge) for i in range(12): for j in range(i): count += 1. What is count?
- (Challenge) for i in range(6): for j in range(i): count += 1. What is count?
Answer key with full working
- Q1: Values 1, 3, … < 16 → Count = 8 → Answer: 8
- Q2: Values 0, 1, … < 11 → Count = 11 → Answer: 11
- Q3: 3 × 4 → Answer: 12
- Q4: 5 × 8 → Answer: 40
- Q5: 0 + 1 + … + 11 → = 66 → Answer: 66
- Q6: 0 + 1 + … + 5 → = 15 → Answer: 15
Syllabus note
This skill is listed under "Flow of Control" for Class 11 in the NCERT/CBSE books. ICSE, IGCSE, IB and state boards may teach it in a different chapter or year.
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.
Related practice and notes
Frequently asked questions
Which chapter is counting loop iterations in?
"Flow of Control", Class 11 Computer Science (NCERT/CBSE). Other boards may place it differently.
How were the answers checked?
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.