Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Use loop else statement to identify when the loop execution is interrupted using a break statement.
  • Implement a loop else statement with a for or a while loop.

Loop else

A loop else statement runs after the loop's execution is completed without being interrupted by a break statement. A loop else is used to identify if the loop is terminated normally or the execution is interrupted by a break statement.

Ex: A for loop that iterates over a list of numbers to find if the value 10 is in the list. In each iteration, if 10 is observed, the statement "Found 10!" is printed and the execution can terminate using a break statement. If 10 is not in the list, the loop terminates when all integers in the list are evaluated, and hence the else statement will run and print "10 is not in the list." Alternatively, a Boolean variable can be used to track whether number 10 is found after loop's execution terminates.

Example 5.4

Finding the number 10 in a list

In the code examples below, the code on the left prints "Found 10!" if the variable i's value is 10. If the value 10 is not in the list, the code prints "10 is not in the list.". The code on the right uses the seen Boolean variable to track if the value 10 is in the list. After loop's execution, if seen's value is still false, the code prints "10 is not in the list.".

numbers = [2, 5, 7, 11, 12]
for i in numbers:
  if i == 10:
    print("Found 10!")
    break
else:
  print("10 is not in the list.")
numbers = [2, 5, 7, 11, 12]
seen = False
for i in numbers:
  if i == 10:
    print("Found 10!")
    seen = True
if seen == False:
  print("10 is not in the list.")
Table 5.3

Checkpoint

Loop else template

Concepts in Practice

Loop else practices

1.
What is the output?
n = 16
exp = 0
i = n
while i > 1:
  if n%2 == 0:
    i = i/2
    exp += 1
  else:
    break
else:
  print(n,"is 2 to the", exp)
  1. 16 is 2 to the 3
  2. 16 is 2 to the 4
  3. no output
2.
What is the output?
n = 7
exp = 0
i = n
while i > 1:
  if n%2 == 0:
    i = i//2
    exp += 1
  else:
    break
else:
  print(n,"is 2 to the", exp)
  1. no output
  2. 7 is 2 to the 3
  3. 7 is 2 to the 2
3.
What is the output?
numbers = [1, 2, 2, 6]
for i in numbers:
  if i >= 5:
    print("Not all numbers are less than 5.")
    break
  else:
    print(i)
    continue
else:
  print("all numbers are less than 5.")
  1. 1
    2
    2
    6
    Not all numbers are less than 5.
    
  2. 1
    2
    2
    Not all numbers are less than 5.
    
  3. 1
    2
    2
    6
    all numbers are less than 5.
    

Try It

Sum of values less than 10

Write a program that, given a list, calculates the sum of all integer values less than 10. If a value greater than or equal to 10 is in the list, no output should be printed. Test the code for different values in the list.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.