Many tasks must be repeated, such as adding the marks of fifty students. A loop repeats a block of statements, either a fixed number of times or until a condition changes.

The while Loop

A while loop repeats its block as long as a condition is True. The condition is tested before each pass, so the loop may run zero times. The block must change something that affects the condition, or the loop will never end. A while loop is used when the number of repetitions is not known in advance, such as reading input until the user types zero.

The for Loop and range()

A for loop visits each item of a sequence in turn, such as the characters of a string or the numbers produced by range(). The call range(n) gives 0 to n - 1, range(a, b) gives a to b - 1, and range(a, b, step) moves by step. The step may be negative to count down. A loop may also have an else part, which runs when the loop finishes without a break.

Definition: Loop. A control structure that repeats a block of statements while a condition holds or for each item of a sequence.

break, continue and pass

The break statement ends the loop at once. The continue statement skips the rest of the current pass and goes to the next one. The pass statement does nothing and is used as a placeholder where a statement is needed but no action is wanted yet. The function exit(), or sys.exit(), stops the whole program. Loops can also be nested, in which the inner loop runs fully for every pass of the outer loop, as in printing a multiplication table or a pattern of stars.

Table 2.2: Loop control statements

Statement Effect Typical use
break Leaves the loop Stop when a value is found
continue Skips to next pass Ignore unwanted values
pass Does nothing Empty block placeholder
exit() Stops the program End on a fatal error

Example 2.2: Sum of numbers with loop control

Problem: Print the sum of the numbers from 1 to 10, skipping multiples of 3, and stop if the running sum exceeds 30.

Solution:

  1. Loop over range(1, 11).
  2. Use continue for multiples of 3.
  3. Add the number, and use break when the sum goes beyond 30.

Result: The loop adds 1, 2, 4, 5, 7 and 8, giving 27. It skips 9, and when 10 is added the sum becomes 37, which is more than 30, so it stops at 10.

total = 0
for n in range(1, 11):
    if n % 3 == 0:
        continue
    total += n
    if total > 30:
        print("stop at", n)
        break
print("total", total)
for i in range(1, 4):
    print("*" * i)

Output

stop at 10
total 37
*
**
***

Common mistakes

  • Forgetting to update the variable in a while loop, which creates an endless loop.
  • Expecting range(5) to include 5. It stops at 4.
  • Using break when continue was meant, which ends the whole loop.

Key takeaways

  • while repeats as long as the condition is True.
  • for visits each item of a sequence.
  • range(a, b) excludes b.
  • break leaves the loop and continue skips one pass.

Practice questions

  1. What does range(2, 8, 2) produce?
  2. What is the difference between break and continue?
  3. Why is the pass statement used?

Answers to the practice questions

  1. 2, 4, 6.
  2. break ends the loop, continue skips the rest of the current pass.
  3. As a placeholder where a statement is required but nothing is to be done.

Sources

  • Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit II