Skip to ContentGo to accessibility page

5.4 Break and continue

Learning objectives

By the end of this section you should be able to

  • Analyze a loop's execution with break and continue statements.
  • Use break and continue control statements in while and for loops.

Break

A break statement is used within a for or a while loop to allow the program execution to exit the loop once a given condition is triggered. A break statement can be used to improve runtime efficiency when further loop execution is not required.

Ex: A loop that looks for the character "a" in a given string called user_string. The loop below is a regular for loop for going through all the characters of user_string. If the character is found, the break statement takes execution out of the for loop. Since the task has been accomplished, the rest of the for loop execution is bypassed.

    user_string = "This is a string."
    for i in range(len(user_string)):
      if user_string[i] == 'a':
        print("Found at index:", i)
        break

Checkpoint

Break statement in a while loop

Break statement in a while loop; ch 5, video 6

Infinite loop

A break statement is an essential part of a loop that does not have a termination condition. A loop without a termination condition is known as an infinite loop. Ex: An infinite loop that counts up starting from 1 and prints the counter's value while the counter's value is less than 10. A break condition is triggered when the counter's value is equal to 10, and hence the program execution exits.

    counter = 1
    while True:
      if counter >= 10:
        break
      print(counter)
      counter += 1

Concepts in Practice

Using a break statement

1.
What is the following code's output?
string_val = "Hello World"
for c in string_val:
  if c == " ":
    break
  print(c)
  1. Hello
  2. Hello World
  3. H
    e
    l
    l
    o
2.
Given the following code, how many times does the print statement get executed?
i = 1
while True:
  if i%3 == 0 and i%5 == 0:
    print(i)
    break
  i += 1
  1. 0
  2. 1
  3. 15
3.
What is the final value of i?
i = 1
count = 0
while True:
  if i%2 == 0 or i%3 == 0:
    count += 1
  if count >= 5:
    print(i)
    break
  i += 1
  1. 5
  2. 8
  3. 30

Continue

A continue statement allows for skipping the execution of the remainder of the loop without exiting the loop entirely. A continue statement can be used in a for or a while loop. After the continue statement's execution, the loop expression will be evaluated again and the loop will continue from the loop's expression. A continue statement facilitates the loop's control and readability.

Checkpoint

Continue statement in a while loop

Control statement in a while loop; ch 5, video 7

Concepts in Practice

Using a continue statement

4.
Given the following code, how many times does the print statement get executed?
i = 10
while i >= 0:
  i -= 1
  if i%3 != 0:
    continue
  print(i)
  1. 3
  2. 4
  3. 11
5.
What is the following code's output?
for c in "hi Ali":
  if c == " ":
    continue
  print(c)
  1. h
    i
    
    A
    l
    i
  2. h
    i
    A
    l
    i
  3. hi
    Ali

Try It

Using break control statement in a loop

Write a program that reads a string value and prints "Found" if the string contains a space character. Else, prints "Not found".

Try It

Using a continue statement in a loop

Complete the following code so that the program calculates the sum of all the numbers in list my_list that are greater than or equal to 5.

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • 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 prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial 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 noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and 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

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.