Home arrow Python arrow Page 2 - Python Statements and Functions

The break Statement - Python

In this seventh part of a nine-part series on the Python language, we continue our discussion of statements and move on to functions. This article is excerpted from chapter four of the book Python in a Nutshell, Second Edition, written by Alex Martelli (O'Reilly; ISBN: 0596100469). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. Python Statements and Functions
  2. The break Statement
  3. The pass Statement
  4. Functions
By: O'Reilly Media
Rating: starstarstarstarstar / 6
October 23, 2008

print this article



The break statement is allowed only inside a loop body. When break executes, the loop terminates. If a loop is nested inside other loops, a break in it terminates only the innermost nested loop. In practical use, a break statement is usually inside some clause of an if statement in the loop body so that break executes conditionally.

One common use of break is in the implementation of a loop that decides whether it should keep looping only in the middle of each loop iteration:

  while True:                   # this loop can never terminate naturally
      x = get_next()
      y = preprocess(x)
      if not keep_looping(x, y): break
      process(x, y)

The continue Statement

The continue statement is allowed only inside a loop body. When continue executes, the current iteration of the loop body terminates, and execution continues with the next iteration of the loop. In practical use, a continue statement is usually inside some clause of an if statement in the loop body so that continue executes conditionally.

Sometimes, a continue statement can take the place of nested if statements within a loop. For example:

  for x in some_container:
      if not seems_ok(x): continue
      lowbound, highbound = bounds_to_test()
      if x<lowbound or x>=highbound: continue
      if final_check(x):

This equivalent code does conditional processing without continue:

  for x in some_container:
if seems_ok(x):
          lowbound, highbound = bounds_to_test()
          if lowbound <= x < highbound:
              if final_check(x):

Both versions function identically, so which one you use is a matter of personal preference and style.

The else Clause on Loop Statements

while and for statements may optionally have a trailing else clause. The statement or block under that else executes when the loop terminates naturally (at the end of the for iterator, or when the while loop condition becomes false), but not when the loop terminates prematurely (via break, return, or an exception). When a loop contains one or more break statements, you often need to check whether the loop terminates naturally or prematurely. You can use an else clause on the loop for this purpose:

  for x in some_container:
if is_ok(x): break         # item x is satisfactory, terminate loop
print "Warning: no satisfactory item was found in container"
x = None

>>> More Python Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: