RG's E-Learning Centre · Book a free demo class · WhatsApp +91-9379416374

Tracing loop output: Class 11 practice

range(a, b, k) produces a, a + k, … stopping before b. Read the method, follow the three worked examples, then try the six questions.

What you should know first

  • Variables and assignment

The idea behind tracing loop output

range(a, b, k) produces a, a + k, … stopping before b. To predict a loop's result, track the variables in a small table, one row per iteration.

Step-by-step method

  1. List the values range() produces.
  2. Update the variables for each value.
  3. Write the final value or printed output.

Worked example 1 (Basic)

What is printed? total = 0 for i in range(2, 10, 3): total += i print(total)

  1. range gives 2, 5, 8
  2. Sum = 15
  3. Answer: 15

Worked example 2 (Standard)

How many times is "hi" printed? for i in range(2, 14, 1): if i % 2 == 0: print("hi")

  1. Values: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
  2. Even values: 2, 4, 6, 8, 10, 12
  3. Answer: 6

Worked example 3 (Challenge)

What is printed? f = 1 i = 1 while i <= 7: f = f * i i += 1 print(f)

  1. Multiplies 1 × 2 × … × 7
  2. = 5040
  3. Answer: 5040

Common mistakes and misconceptions

  • Including the stop value of range
  • Forgetting to update an accumulator

Practice questions

  1. (Basic) What is printed? total = 0 for i in range(1, 17, 1): total += i print(total)
  2. (Basic) What is printed? total = 0 for i in range(5, 14, 3): total += i print(total)
  3. (Standard) How many times is "hi" printed? for i in range(5, 10, 1): if i % 2 == 0: print("hi")
  4. (Standard) How many times is "hi" printed? for i in range(1, 16, 1): if i % 2 == 0: print("hi")
  5. (Challenge) What is printed? f = 1 i = 1 while i <= 3: f = f * i i += 1 print(f)
  6. (Challenge) What is printed? f = 1 i = 1 while i <= 6: f = f * i i += 1 print(f)

Answer key with full working

  • Q1: range gives 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 → Sum = 136 → Answer: 136
  • Q2: range gives 5, 8, 11 → Sum = 24 → Answer: 24
  • Q3: Values: 5, 6, 7, 8, 9 → Even values: 6, 8 → Answer: 2
  • Q4: Values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 → Even values: 2, 4, 6, 8, 10, 12, 14 → Answer: 7
  • Q5: Multiplies 1 × 2 × … × 3 → = 6 → Answer: 6
  • Q6: Multiplies 1 × 2 × … × 6 → = 720 → Answer: 720

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 tracing loop output 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.

Book a free demo class