Break and Continue Statement in Python W3Schools Tutorial

Logan Jul 13, 2026

Break and continue statements in Python are control flow constructs used to alter the normal sequence of execution of a loop. These statements come quite handy when you want to skip to the next iteration of a loop or immediately exit out of a loop without executing the remaining statements in the current iteration.

Use of break and continue in Python with Examples - EasyCodeBook.com
Use of break and continue in Python with Examples - EasyCodeBook.com

In this guide, we're going to explore these statements in detail, starting with the 'break' statement. We'll walk through how to use them in Python, provide practical examples, and discuss their use cases with tips on best practices. Let's dive into the world of break and continue statements to make your coding journey more efficient and effective.

Python while Loops: Repeating Tasks Conditionally – Real Python
Python while Loops: Repeating Tasks Conditionally – Real Python

Python Break Statement

The 'break' statement is used to exit from the loop whenever a certain condition is met. It results in skipping the remaining instructions in the current loop and moving on to the next loop (if there is any) or exiting the program (if the 'break' is present within the outermost loop).

Python Conditional Statements & Loops
Python Conditional Statements & Loops

Let's take a look at how to use the 'break' statement in Python.

Syntax Description
break Exits from the closest enclosing loop.
break vs continue in Python (Don’t Confuse ❌)
break vs continue in Python (Don’t Confuse ❌)

Finding a Specific Number in a List

We can use 'break' to exit a loop as soon as we find the value we're looking for. Here's an example.

```python numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for num in numbers: if num == 7: print("Number 7 found!") break else: print("Still searching...") ```

In this example, once we find the number 7, we'll print the message and then exit the loop with 'break'.

Python Try and Except | Finally | Else | Raise Statements ⋆ IpCisco
Python Try and Except | Finally | Else | Raise Statements ⋆ IpCisco

Infinite Loops

'Break' can also be used to exit infinite loops that might not have a natural endpoint.

```python while True: user_input = input("Type 'quit' to exit the loop: ") if user_input.lower() == "quit": break else: print("You typed:", user_input) ```

In this Greek way of writing "while true", we've included an exit condition, so it won't truly run infinitely. When the user types 'quit', the input is read into the variable and the 'break' statement is executed on that condition, exiting the loop.

the if statement in python is true or false, and it's very easy to read
the if statement in python is true or false, and it's very easy to read

Python Continue Statement

The 'continue' statement in Python is used to skip the rest of the code inside a loop for the current iteration only. The loop does not terminate but immediately jumps to the next iteration, making programming more flexible and efficient.

| Pythonista Planet
| Pythonista Planet
Skip Ahead in Loops With Python's continue Keyword – Real Python
Skip Ahead in Loops With Python's continue Keyword – Real Python
Built-in Functions of Python Strings
Built-in Functions of Python Strings
🐍 Python – break Statement (Complete Beginner Guide)
🐍 Python – break Statement (Complete Beginner Guide)
a computer screen with the words break, continue and pass
a computer screen with the words break, continue and pass
How Python Break for loop With Examples
How Python Break for loop With Examples
Python if-else statement guide
Python if-else statement guide
Python Continue Statement (Complete Guide for Beginners)
Python Continue Statement (Complete Guide for Beginners)
a screenshot of a computer screen with text on it that reads, using break to stop a loop using continue to skip one value
a screenshot of a computer screen with text on it that reads, using break to stop a loop using continue to skip one value
Python Conditional Statements Cheat Sheet
Python Conditional Statements Cheat Sheet
Multiple-line Comment in Python with Best Examples
Multiple-line Comment in Python with Best Examples
10 Fun Python Exercises for Kids
10 Fun Python Exercises for Kids
Coding For Beginners Python - Learn the Basics - Conditionals
Coding For Beginners Python - Learn the Basics - Conditionals
Conditional Statements in Python - The Engineering Projects
Conditional Statements in Python - The Engineering Projects
Python for Beginners: 8 Critical Mistakes to AVOID! 🐍🛑
Python for Beginners: 8 Critical Mistakes to AVOID! 🐍🛑
a computer screen with the text example 2 stopping at a specific number using break to stop the loop when encountering the number s
a computer screen with the text example 2 stopping at a specific number using break to stop the loop when encountering the number s
7 Cool Python Projects for Kids to Try!
7 Cool Python Projects for Kids to Try!
Text-Based Adventure Game in Python (Complete Guide)
Text-Based Adventure Game in Python (Complete Guide)
9 Python Projects for Beginners (Build Your Portfolio Fast)
9 Python Projects for Beginners (Build Your Portfolio Fast)
Top 25 Python Interview Questions and Answers (Beginner Friendly)
Top 25 Python Interview Questions and Answers (Beginner Friendly)

Let's take a look at the syntax and some examples of using continue in Python.

Syntax Description
continue Skips the rest of the code inside the loop for the current iteration and moves on to the next iteration.

Skipping a Number in a Loop

'Continue' can be used to skip certain values in a numeric loop. For example, let's say we want an even numbers counter:

```python for num in range(20): if num % 2 != 0: continue print(num) ```

Here, when 'continue' is encountered, it skips over any odd numbers, so only even numbers are printed.

Removing Elements from a List While Looping

You can remove items from a list while looping over it by using 'continue'. Here's an example.

```python words = ["apple", "banana", "kiwi", "cherry"] for word in words: if len(word) < 5: continue print(word) ```

In this scenario, words with less than five characters are skipped over.

For an engaging, interactive experience with break and continue statements, head over to W3Schools Python Tutorials for more practical examples and guided learning.

Remember, although these control flow statements are powerful tools, they should be used judiciously to avoid causing confusion in your code. Always ensure that your 'break' and 'continue' conditions are clearly defined and used sparingly.

Now, you're ready to harness the power of break and continue statements in your Python coding expeditions. Happy coding!