MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 11 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python9 min · Section 11 of 12

10.11Loops: while, for, break and continue

Covers IGNOU MCS-201 §10.9.3

Loops repeat a block of statements. Python has two: while, which repeats as long as a condition holds, and for, which goes through the items of a sequence or other iterable one by one.

The while loop

Syntax
while condition:
    block

The condition is tested before each iteration; when it is false, the loop ends and execution continues after the loop. The block must eventually make the condition false (usually by changing an iteration variable), otherwise the loop runs forever. while is an indefinite loop: the number of iterations is not known in advance.

The for loop

Syntax
for variable in iterable:
    block

The variable takes each item of the iterable in turn: the items of a list, tuple or set, the characters of a string, the keys of a dictionary, the lines of a file, or the numbers produced by range(). for is a definite loop: it runs once per item.

range()

range(stop), range(start, stop), range(start, stop, step) produce integers from start (default 0) up to but not including stop, moving by step (default 1; may be negative but not zero).

CallNumbers produced
range(5)0, 1, 2, 3, 4
range(2, 6)2, 3, 4, 5
range(1, 10, 3)1, 4, 7
range(10, 0, -2)10, 8, 6, 4, 2

break, continue and else

StatementEffect
breakLeave the loop at once; continue after the loop
continueSkip the rest of this iteration and go to the next one
else on a loopRuns once when the loop ends normally, not when it ends by break

Key takeaways

  • while is condition-controlled; for is item-controlled.
  • range(start, stop, step) excludes stop.
  • break ends the loop; continue skips to the next iteration.
  • A loop’s else runs only when the loop was not ended by break.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does list(range(3, 12, 4)) give?

Show answer

Answer: C. [3, 7, 11]

Start at 3 and add 4 while staying below 12: 3, 7, 11.

Question 2 · multiple choice

What does this program print?

Python · quiz_while.py
n = 5
while n > 0:
    n -= 2
print(n)
Show answer

Answer: C. -1

n goes 5 → 3 → 1 → −1; at −1 the condition n > 0 is false and the loop ends.

Question 3 · multiple choice

What does this program print?

Python · quiz_break.py
for i in range(1, 6):
    if i == 3:
        continue
    if i == 5:
        break
    print(i, end=" ")
Show answer

Answer: B. 1 2 4

3 is skipped by continue; at 5 the loop breaks before printing.

Question 4 · multiple choice

When does the else block of a for loop run?

Show answer

Answer: C. Once, when the loop finishes without a break

The loop else means “completed normally”. It is skipped when break ends the loop.

Question 5 · multiple choice

Which statement about loops is false?

Show answer

Answer: D. A while loop runs until its condition becomes true

It is the reverse: a while loop stops when its condition becomes false.

Question 6 · short answer

Write a program that reads numbers until the user enters 0 and then prints their sum and count, and say which loop you chose and why.

Show worked answer

total = count = 0, then while True: n = int(input("Number (0 to stop): ")), if n == 0: break, total += n, count += 1, and finally print("Sum:", total, "Count:", count). A while loop is chosen because the number of inputs is not known in advance (an indefinite loop); break ends it when the sentinel value 0 is entered, and 0 itself is not added.

↑ ↓ move↵ openTab switch type