Chapter 15 — Iterative Statements (Loops)

Easy-language Notes · Part 1 of 2 (for · range · while · conversion)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Front)

1. The for Loop & range()

A loop repeats a block of code several times. The for loop is the simplest — it walks over a sequence of values, one at a time.

Think of a teacher calling out roll numbers 1, 2, 3, …, 30. Each call is one round of the loop, and the roll number is the loop variable. When the list is done, the loop stops.

Syntax

for variable in sequence:
    # indented block runs once per item
    statement

The range() function

range() produces a stream of numbers without storing them all in memory. It has three forms:

FormProduces
range(n)0, 1, 2, …, n−1
range(a, b)a, a+1, …, b−1
range(a, b, s)a, a+s, a+2s, … (stops before b)
Print 1 to 5 squared:
for i in range(1, 6):
    print(i, i*i)
Output: 1 1 / 2 4 / 3 9 / 4 16 / 5 25

Looping over a sequence

A for loop can also walk through any string, list, or tuple:

for ch in "Python":
    print(ch, end=" ")
Output: P y t h o n
range(a, b) never includes b. To go from 1 to 10 inclusive, write range(1, 11). To count down, use a negative step: range(10, 0, -1).

2. The while Loop

A while loop repeats as long as a condition stays True. It is used when you don't know in advance how many rounds are needed.

Eating biscuits from a packet — "while there are biscuits, keep eating." You don't know how many biscuits are inside; you stop when the packet is empty.

Syntax

while condition:
    # body runs as long as condition is True
    update_the_variable
Print 5 down to 1:
n = 5
while n > 0:
    print(n, end=" ")
    n -= 1
Output: 5 4 3 2 1
Sum digits of a number:
num, total = 1234, 0
while num > 0:
    total += num % 10
    num //= 10
print(total)        # 10

Three things every while needs

  1. Initialise the variable before the loop.
  2. Test the condition in the while header.
  3. Update the variable inside the body so the condition eventually becomes False.
If the update step is missing, the condition never becomes False and you get an infinite loop. Press Ctrl + C to stop a runaway program.

3. for ↔ while — When & Conversion

Both loops can do the same job, but each fits some problems better than the other.

Use for when…Use while when…
The number of rounds is known.You stop when something changes.
You walk through a list / string.You wait for a user signal.
Counting up or down a range.Reading input until a sentinel value.
Same task in both styles — print 1 to 5:
# for version
for i in range(1, 6):
    print(i)

# while version
i = 1
while i <= 5:
    print(i)
    i += 1
The for form is shorter; the while form makes the counter changes visible.

Conversion recipe (for → while)

  1. Pick a counter and initialise it to the start value.
  2. Put the stop test as the while condition.
  3. Add a step update at the bottom of the body.
Whenever you write a while, look at it again and ask: "Could a for with range() say the same thing in one line?" If yes, prefer the for — it is harder to mess up.

Chapter 15 — Iterative Statements (Loops)

Easy-language Notes · Part 2 of 2 (break · continue · nested · patterns)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Back)

4. break, continue and the loop else

Three small keywords change how a loop behaves once it has started.

break — quit the loop early

Find the first number divisible by 7:
for n in range(20, 50):
    if n % 7 == 0:
        print("Found", n)
        break
Output: Found 21 — the loop stops immediately, even though numbers 22 to 49 remain.

continue — skip this round, keep looping

Print only odd numbers from 1 to 8:
for i in range(1, 9):
    if i % 2 == 0:
        continue
    print(i, end=" ")
Output: 1 3 5 7

The loop else clause

A for or while loop can have an else. It runs when the loop ends normally (without a break).

for n in range(2, 10):
    if n == 5:
        break
else:
    print("never broke")
Nothing prints — the break ran. Without the if, "never broke" would print.
break jumps out completely; continue only skips the current round. The loop else is unusual — remember it triggers only when no break happened.

5. Nested Loops

A loop placed inside another loop. The inner loop runs in full for every single round of the outer loop.

Two clocks — the minute hand and the hour hand. For every one move of the hour hand, the minute hand goes round 60 times. Outer = hour, inner = minute.

Counting iterations

If the outer loop runs m times and the inner runs n times, the inner body runs m × n times.

Print a 3-by-4 grid of coordinates:
for r in range(3):
    for c in range(4):
        print((r, c), end=" ")
    print()
12 pairs are printed in 3 rows.
A right-angled triangle of stars:
for i in range(1, 5):
    for j in range(i):
        print("*", end="")
    print()
Output:
*
**
***
****
A break inside a nested loop only exits the inner loop. The outer loop keeps running. Use a boolean flag if you need to escape both.

6. CBSE Suggested Programs (Cheat Sheet)

The chapter ends with a handful of standard loop programs every student must know by heart. The patterns repeat in many other problems.

Summation of a series

Sum 1 + 2 + … + n:
n = int(input("n: "))
total = 0
for i in range(1, n + 1):
    total += i
print(total)

Factorial of n

n = 5
fact = 1
for i in range(1, n + 1):
    fact *= i
print(fact)       # 120

Fibonacci series (first n terms)

n = 7
a, b = 0, 1
for _ in range(n):
    print(a, end=" ")
    a, b = b, a + b
Output: 0 1 1 2 3 5 8

Prime check

n = 17
prime = True
for d in range(2, n):
    if n % d == 0:
        prime = False
        break
print("Prime" if prime else "Not prime")
In every "accumulator" loop — sum, product, count, max, min — initialise the accumulator before the loop. total = 0 for a sum, fact = 1 for a product. Initialising inside the loop resets it every round and gives the wrong answer.

Chapter 15 Worksheet — Iterative Statements (Loops)

Total: 50 marks  |  Time: 75 min
Name: Class/Sec: Roll No.: Date:

Section A — Multiple Choice (1 × 10 = 10)

  1. Which keyword starts a counted loop in Python?
    (a) loop(b) for(c) repeat(d) do
  2. range(2, 10, 3) produces:
    (a) 2, 5, 8(b) 2, 5, 8, 11(c) 2, 4, 6, 8(d) 3, 6, 9
  3. What is the output of the snippet?
    for i in range(3):
        print(i, end="")
    (a) 012(b) 123(c) 0123(d) Error
  4. break inside a loop causes:
    (a) Skip a round(b) Exit the loop(c) Restart(d) Pause
  5. continue inside a loop causes:
    (a) Exit the loop(b) Restart program(c) Skip to next round(d) Stop interpreter
  6. How many times does this body run?
    for i in range(2):
        for j in range(3):
            print("x")
    (a) 5(b) 6(c) 8(d) 9
  7. The loop else block runs when:
    (a) The loop body fails(b) break happens(c) The loop ends normally(d) Never
  8. An infinite while loop usually happens because:
    (a) Wrong indentation(b) Missing colon(c) Variable never changes(d) Too many prints
  9. Which call counts down 10, 9, 8, …, 1?
    (a) range(10, 0)(b) range(10, 0, -1)(c) range(1, 11)(d) range(10, −1, −1)
  10. Inside for ch in "AI":, the variable ch takes the values:
    (a) "AI"(b) A, I(c) 0, 1(d) Error

Section B — Find the Output (1 × 8 = 8)

  1. for i in range(1, 5):
        print(i*i, end=" ")
    Output:
  2. n = 4
    while n > 0:
        print(n, end=" ")
        n -= 1
    Output:
  3. t = 0
    for i in range(1, 6):
        t += i
    print(t)
    Output:
  4. for i in range(1, 10):
        if i == 5:
            break
        print(i, end="")
    Output:
  5. for i in range(1, 7):
        if i % 2 == 0:
            continue
        print(i, end="")
    Output:
  6. for r in range(1, 4):
        for c in range(r):
            print("*", end="")
        print()
    Output:
  7. a, b = 0, 1
    for _ in range(5):
        print(a, end=" ")
        a, b = b, a + b
    Output:
  8. k = 1
    while k < 50:
        k *= 3
    print(k)
    Output:

Section C — Find the Error (1 × 6 = 6)

Each snippet has exactly one error. Mark the line number and write the corrected line.

  1. 1  for i in range(5)
    2      print(i)
    Wrong line:   Correction:
  2. 1  for i in range(5):
    2  print(i)
    Wrong line:   Correction:
  3. 1  n = 5
    2  while n > 0:
    3      print(n)
    Wrong line:   Correction:
  4. 1  for i in range(1, 5):
    2      print(i)
    3   print("end")
    Wrong line:   Correction:
  5. 1  for i in range(10, 1):
    2      print(i)
    Wrong line:   Correction:
  6. 1  i = 0
    2  while i < 5
    3      print(i)
    4      i += 1
    Wrong line:   Correction:

Section D — Short Answer (2 × 5 = 10)

  1. Differentiate for and while with one example each.

Chapter 15 Worksheet — (continued)

Sheet 2 (Back)

Section D — Short Answer (continued) (2 × 5 = 10)

  1. What is the difference between break and continue? Give one example of each.
  2. Explain the three pieces every while loop needs (initialise, test, update). Show an example with all three labelled.
  3. Convert the following for loop into an equivalent while loop:
    for i in range(2, 11, 2):
        print(i)
  4. What does the else clause of a loop do? Write one example where it runs and one where it does not.
  5. How many times does the inner print run in a nested loop where the outer goes 1 to 5 and the inner goes 1 to 4? Show your working.

Section E — Code Tasks (16 marks)

Each program should read input from the user (where required) and produce a clear output. Use a separate sheet if you need more space.

  1. (3) Read an integer n and print the sum of the first n natural numbers using a for loop with range().
  2. (3) Read a positive integer n and print its factorial using a while loop. Reject n < 0 with a clear message.
  3. (3) Print the multiplication table of any number entered by the user, in the format 5 x 1 = 5, …, 5 x 10 = 50. Use a for loop.
  4. (4) Read an integer n and print a right-angled star triangle of height n:
    *
    **
    ***
    ****
    Use nested loops. Then modify it to print a mirror triangle (right-aligned) using spaces.
  5. (3) Read an integer n and decide whether it is prime. Use a for loop with break; print "Prime" or "Not prime".