You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

5.4 Break and continue

2 min readjune 24, 2024

statements are powerful tools in Python programming. They allow you to fine-tune the execution of loops, giving you precise control over when to exit or skip iterations. This level of control is essential for writing efficient and flexible code.

and statements serve different purposes in loop control. While lets you exit a loop prematurely, continue allows you to skip specific iterations. Understanding when and how to use these statements can greatly enhance your ability to write effective Python programs.

Loop Control Statements

Break statements for loop exits

Top images from around the web for Break statements for loop exits
Top images from around the web for Break statements for loop exits
  • break
    statement prematurely exits a loop (for or while) when a specific condition is met
  • Immediately terminates the loop and continues with the next statement after the loop
  • Often used with conditional statements (if, elif) to specify exit conditions
  • Example:
    for i in range(1, 10):
        if i == 5:
            break
        print(i)
    
    • Iterates from 1 to 9, but when
      i
      becomes 5,
      break
      is executed and loop terminates
    • Output: 1, 2, 3, 4
  • Provides a way to implement within loops

Continue statements for iteration skips

  • continue
    statement skips the rest of the current within a loop and moves to the next iteration
  • Immediately jumps to the next iteration, skipping remaining code in current iteration
  • Often used with conditional statements (if, elif) to specify skip conditions
  • Example:
    for i in range(1, 6):
        if i == 3:
            continue
        print(i)
    
    • Iterates from 1 to 5, but when
      i
      becomes 3,
      continue
      is executed and rest of iteration is skipped
    • Output: 1, 2, 4, 5
  • Enables within loops

Break vs continue in loops

  • break
    and
    continue
    have different effects on loop execution flow
    • break
      terminates the entire loop prematurely and transfers control to next statement after loop
    • continue
      skips rest of current iteration and moves to next iteration within same loop
  • When
    break
    is encountered:
    • Loop is immediately exited, regardless of remaining iterations
    • Program continues with next statement after loop
  • When
    continue
    is encountered:
    • Current iteration is skipped, remaining code in that iteration not executed
    • Loop proceeds to next iteration, continuing execution until loop condition no longer satisfied or
      break
      encountered
  • Use
    break
    and
    continue
    appropriately based on desired behavior
    • break
      to exit loop entirely based on specific condition
    • continue
      to skip certain iterations based on specific condition but continue with rest of loop

Loop Optimization and Readability

  • Break and continue statements can be used for by avoiding unnecessary iterations
  • Proper use of these statements can improve by simplifying complex loop structures
  • using break and continue should be used judiciously to maintain clear program flow
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary