Skip to content
Computer ScienceCBSE Class 11 · Flow of Control

Loops in Python Class 11: for, while, break, continue and Nested Patterns

Updated 17 October 2026 · Fact-checked

A for loop repeats over a sequence such as range(); a while loop repeats as long as its condition is True. range(start, stop, step) generates numbers from start up to but not including stop. break exits the loop at once; continue skips the rest of the current pass. A nested loop runs the inner loop completely for each pass of the outer loop.

For each value of i the inner loop prints i stars, then print() moves to the next row, so the rows grow 1, 2, 3, 4.

Illustration drawn for QuestionBuddy.

Understand for and while Loops, break, continue and Nested Loop Patterns

range(). range(5) gives 0, 1, 2, 3, 4. range(2, 8) gives 2 to 7. range(2, 11, 3) gives 2, 5, 8 (stop 11 excluded). range(10, 0, -3) gives 10, 7, 4, 1.

for loop.

for i in range(2, 11, 3):
    print(i, end=" ")

Output: 2 5 8 (a space follows each number).

while loop (digit sum).

n = 4705
s = 0
while n > 0:
    s += n % 10
    n //= 10
print(s)

n takes 4705, 470, 47, 4; digits added 5, 0, 7, 4; output 16.

continue.

total = 0
for i in range(1, 6):
    if i == 4:
        continue
    total += i
print(total)

Output: 11 (1 + 2 + 3 + 5).

break and loop else.

for i in range(1, 20):
    if i % 7 == 0:
        print(i)
        break
else:
    print("none")

Prints 7; the else belongs to the loop and runs only if the loop finishes without break. With range(8, 13) the output is none.

Prime test.

n = 29
is_prime = True
for d in range(2, n):
    if n % d == 0:
        is_prime = False
        break
print(is_prime)

Output: True (valid for n > 1).

Nested loops: star triangle.

for i in range(1, 5):
    for j in range(i):
        print("*", end="")
    print()

Output:

*
**
***
****

The outer loop controls the rows; the inner loop prints i stars; print() moves to the next line.

Fibonacci (first 8 terms).

a, b = 0, 1
for _ in range(8):
    print(a, end=" ")
    a, b = b, a + b

Output: 0 1 1 2 3 5 8 13 .

Counting loop. x = 1, while x < 50: x = x * 3, print(x) gives 3, 9, 27, 81 and prints 81.

for and while Loops, break, continue and Nested Loop Patterns: key facts

range
rangestop
rangestart, stop
rangestart, stop, step
Stop is never included
Negative step counts down.
Loops
  • for: fixed sequence
  • while: condition-controlled
  • Both need an indented body
Avoid infinite loops.
Jump statements
  • break: leave the loop
  • continue: skip to the next pass
  • loop else runs if no break occurs
Only inside loops.
Nested loops
Total passes = outer passes x inner passes | Inner loop runs fully for each outer pass
Used for patterns.

How to answer for and while Loops, break, continue and Nested Loop Patterns questions

Trace loops with a table of variable values.

  1. 1Write the initial values of all variables.
  2. 2For each pass write the loop variable and the condition result.
  3. 3Update variables in the order of the statements.
  4. 4For break, stop the table; for continue, skip to the next row.
  5. 5Write the output exactly, noting end and print() calls.

for and while Loops, break, continue and Nested Loop Patterns 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.

Digits of a ticket number

Suppose a ticket number 4705 must have its digits added to form a check digit.

Which loop and what result?

Try it first, then show the solutionSolution
  1. A while loop takes the last digit with n % 10 and removes it with n //= 10.
  2. Digits added: 5 + 0 + 7 + 4.

Answer: Check sum 16.

Memory hook: Take the last digit, drop it, repeat.

Tricks to remember for and while Loops, break, continue and Nested Loop Patterns

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Stop is never included

    range goes up to but not including stop.

    Use it when: range().

  • Break leaves, continue skips

    break exits; continue goes to the next pass.

    Use it when: Jump statements.

for and while Loops, break, continue and Nested Loop Patterns: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: range output · 1 mark · MCQBoard level

What is the output?

for i in range(3, 14, 4):
    print(i, end=" ")

(a) 3 7 11 15
(b) 3 7 11
(c) 3 4 5
(d) 7 11 14

Try it first, then show the solutionSolution
  1. Values 3, 7, 11; the next would be 15, beyond the stop value 14.

Answer: (b) 3 7 11

Example 2: continue · 3 marks · Short answerBoard level

What is the output of the code?

total = 0
for i in range(1, 6):
    if i == 4:
        continue
    total += i
print(total)

(3 marks)

Try it first, then show the solutionSolution
  1. i takes 1 to 5; when i is 4 the pass is skipped.
  2. total = 1 + 2 + 3 + 5.
  3. Output: 11.

Answer: Output: 11.

Example 3: Digit sum · 4 marks · Short answerHarder board level

Trace the program with n = 4705 and give its output and the values of n at each pass:

n = 4705
s = 0
while n > 0:
    s += n % 10
    n //= 10
print(s)

(4 marks)

Try it first, then show the solutionSolution
  1. Pass 1: s = 5; n = 470. Pass 2: s = 5; n = 47. Pass 3: s = 12; n = 4. Pass 4: s = 16; n = 0.
  2. Condition n > 0 is False: the loop ends.
  3. Output: 16.

Answer: Output: 16.

Example 4: break with else · 4 marks · Short answerHarder board level

What is the output of each program? Explain the role of else.
(i)

for i in range(1, 20):
    if i % 7 == 0:
        print(i)
        break
else:
    print("none")

(ii) the same program with range(8, 13). (4 marks)

Try it first, then show the solutionSolution
  1. (i) First multiple of 7 is 7: prints 7 and break skips the loop else.
  2. (ii) range(8, 13) gives 8 to 12, none divisible by 7; the loop finishes without break, so else prints none.
  3. The else block of a loop runs only if the loop ends normally, without break.

Answer: (i) 7; (ii) none.

Example 5: Pattern · 5 marks · Short answerHOTS / competency

Write a program to print the pattern for n = 4:
1
1 2
1 2 3
1 2 3 4
and count how many numbers are printed in total. (5 marks)

Try it first, then show the solutionSolution
  1. Code: for i in range(1, 5): for j in range(1, i + 1): print(j, end=" ") then print().
  2. Row i prints i numbers: 1 + 2 + 3 + 4.
  3. Total = 10 numbers printed.
  4. Each row ends with print() to move to the next line.

Answer: Pattern printed; total 10 numbers.

Where students lose marks in for and while Loops, break, continue and Nested Loop Patterns

The slips examiners see most often, and what to write instead.

  • Mistake: Including the stop value

    Forgetting that stop is excluded.

    Wrong working

    range(1, 5) gives 1 to 5.

    Right working

    range(1, 5) gives 1, 2, 3, 4.

    Do this: Stop is never included.

    Usually costs: 1 mark

  • Mistake: Infinite while loop

    Not changing the condition variable.

    Wrong working

    while n > 0: s += n % 10

    Right working

    Add n //= 10 inside the loop.

    Do this: Update the variable in the body.

    Usually costs: 1 mark

  • Mistake: Missing print() after the inner loop

    Pattern on one line.

    Wrong working

    Stars all on one line.

    Right working

    print() after the inner loop moves to the next line.

    Do this: Match rows with print().

    Usually costs: 1 mark

Exam tips

  • Use a table of variable values while tracing.
  • Check the first and last pass carefully.
  • Quote exact output including trailing spaces when end=" " is used.
  • State the final value of the loop variable if asked.

Practice questions on for and while Loops, break, continue and Nested Loop Patterns

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    Which statement skips the remaining statements of the current loop pass?
    (a) break
    (b) continue
    (c) pass
    (d) exit

    Hint

    Next pass.

    Answer

    (b) continue

  2. Q2 · 3 marks · Short answerHarder board level

    What is the output?

    x = 1
    while x < 50:
        x = x * 3
    print(x)

    (3 marks)

    Hint

    x goes 1, 3, 9, 27, 81.

    Answer

    x becomes 3, 9, 27 and 81; 81 < 50 is False so the loop ends. Output: 81.

  3. Q3 · 4 marks · Short answerHarder board level

    Write a program that prints all multiples of 4 below 30 on one line and then their sum on the next line. Give the output. (4 marks)

    Hint

    range with step 4.

    Answer

    total = 0; for i in range(4, 30, 4): print(i, end=" "); total += i; then print(); print("Sum:", total). Output: 4 8 12 16 20 24 28 (space after each) and on the next line Sum: 112.

  4. Q4 · 1 mark · Assertion-reasonHarder board level

    Assertion (A): The else block of a for loop executes when the loop ends without break.
    Reason (R): The else clause of a loop is skipped if break is executed.
    (a) Both A and R are true, and R is the correct explanation of A.
    (b) Both A and R are true, but R is not the correct explanation of A.
    (c) A is true, but R is false.
    (d) A is false, but R is true.

    Hint

    Loop else.

    Answer

    (a) Both A and R are true, and R is the correct explanation of A.

  5. Q5 · 5 marks · Short answerHOTS / competency

    Write a program to check whether n = 29 is prime and then modify it to print all prime numbers from 2 to 20. State the output of the modified program. (5 marks)

    Hint

    Nested loop with break.

    Answer

    for n in range(2, 21): is_prime = True; for d in range(2, n): if n % d == 0: is_prime = False; break; if is_prime: print(n, end=" "). Output: 2 3 5 7 11 13 17 19 (n = 29 gives True).

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. 1.Does range(5) include 5?Show answer

    No; it gives 0 to 4.

  2. 2.What does break do?Show answer

    Exits the loop immediately.

  3. 3.When does a loop else run?Show answer

    When the loop finishes without break.

  4. 4.Which loop depends on a condition?Show answer

    while.

  5. 5.What does print() with no argument do?Show answer

    Prints a new line.

for and while Loops, break, continue and Nested Loop Patterns: frequently asked questions

What is an infinite loop?

A loop whose condition never becomes False.

Can a for loop have a step?

Yes, with range(start, stop, step).

What is a nested loop?

A loop inside another loop.