GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python break, continue statement

  • Home
  • ▼Introduction to Python
  • Python Home
  • Python 2 vs 3
  • Python Installation
  • Python IDLE
  • ▼Python Basic
  • Python Syntax
  • Python Print Statement
  • Python Variable
  • Python Data Type
  • Python Operators
  • Python If elif else
  • Python For Loop
  • Python While Loop
  • Python break, continue
  • ▼Data Handling and Structures
  • Python String
  • Python Lists
  • Python Dictionary
  • Python Tuples
  • Python Sets
  • Python Bytes, Bytearray
  • Python Advanced Data Structure
  • ▼Date and Time
  • Python Date and Time
  • Python Calendar Module
  • ▼Functions and Modules
  • Python User define function
  • Python Built-in Functions
  • Python Module
  • Python Functions Examples
  • ▼File Handling and I/O
  • Python File Handling
  • ▼Object-Oriented Programming
  • Python Classes
  • ▼Advanced Programming Constructs
  • Python Iterators
  • Python Generators
  • Python Functional Programming
  • Python Multithearding
  • ▼Regular Expressions
  • Python Regular Expressions
  • ▼Error Handling and Debugging
  • Python Exception Handling
  • Python Testing and Debugging
  • ▼Database Interaction
  • Python SQlite Database
  • Python SQLalchemy ORM
  • ▼Advanced Python Topics
  • Advanced Python Topics
  • Python Standard Library
  • ▼Web Programming
  • CGI Programming
  • ▼Python Miscellaneous
  • Python-docx: Create and Manipulate
  • Software Frameworks
  • Floor Division in Python
  • Thonny Python IDE
  • pip install mysql-connector-python
  • Astral Python in 2026
  • PyCharm 2026 The Ultimate Guide
  • Python for Data Analysis in 2026
  • Self-Driving Python Server
  • Python-Native Hypermedia UI
  • Building Dashboards with Pythons
  • Building an Educational HTTP Server
  • Python-First Rapid App Generator
  • Markdown-Native Personal Site Framework
  • pyRAP Framework
  • Hypermedia-Driven Python Framework
  • Shinylives Reactive Framework
  • ASGIMachine: HTTP Decision Graph Framework
  • Embedded Web Framework for MicroPython
  • ..More to come..

Python break, continue statement

Last update on April 14 2026 12:43:45 (UTC/GMT +8 hours)

break statement

The break statement is used to exit a for or a while loop. The purpose of this statement is to end the execution of the loop (for or while) immediately and the program control goes to the statement after the last statement of the loop. If there is an optional else statement in while or for loop it skips the optional clause also. Here is the syntax.

Syntax:

 
			  while (expression1) :
     statement_1 
     statement_2
     ......
     if expression2 :
     break
for variable_name in sequence :
   statement_1 
   statement_2
   if expression3 :
     break

Example: break in for loop

In the following example for loop breaks when the count value is 5. The print statement after the for loop displays the sum of first 5 elements of the tuple numbers.

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
num_sum = 0
count = 0
for x in numbers:
    num_sum = num_sum + x
    count = count + 1 
    if count == 5:
        break
print("Sum of first ",count,"integers is: ", num_sum)

Output:

Sum of first  5 integers is:  15

Example: break in while loop

In the following example while loop breaks when the count value is 5. The print statement after the while loop displays the value of num_sum (i.e. 0+1+2+3+4).

num_sum = 0
count = 0
while(count<10):
    num_sum = num_sum + count
    count = count + 1 
    if count== 5:
        break
print("Sum of first ",count,"integers is: ", num_sum)

Output:

Sum of first  5 integers is :  10

continue statement

The continue statement is used in a while or for loop to take the control to the top of the loop without executing the rest statements inside the loop. Here is a simple example.

for x in range(7):
    if (x == 3 or x==6):
        continue
    print(x)

Output:

0
1
2
4
5

In the above example, the for loop prints all the numbers from 0 to 6 except 3 and 6 as the continue statement returns the control of the loop to the top

PREV : Python While Loop
NEXT : Python String

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.