Break and continue commands in Python

In Python , the break and continue statements can change the "flow" of a normal loop.

These loops repeat a code block until the condition checks False, but sometimes we want to terminate the current loop or even the entire loop without checking the conditional expression. That's when we need the help of break and continue commands.

Break command in Python

The break statement ends the loop containing it and passes the control to the next command after that loop's code block. If the break statement is in a nested loop (loop inside another loop), break will terminate the innermost loop.

Syntax of break order:

 break 

Break order diagram:

Break and continue commands in Python Picture 1

If using break in the for Python loop, it will look like this:

 for var in sequence: 

#khối code bên trong vòng lặp for

if dieu_kien:

break

#code khác bên trong vòng lặp for

#code bên ngoài vòng lặp for

When break is executed, "#code inside the for loop" will be ignored and redirected to "#code outside the for loop".

If you use the break in the while loop, Python will look like this:

 while dieu_kien_kiem_tra: 

#code bên trong vòng lặp while

if dieu_kien:

break

#code khác bên trong vòng lặp while

#code bên ngoài vòng lặp while

When break is executed, "#code inside while loop" will be ignored and redirected to "#code outside the while loop".

Example of a break Python command

Example 1:

 #Sử dụng break trong for 

for val in "string":
if val == "i":
break
print(val)

print("Kết thúc!")

In the above code, we loop the string "string", and check the condition, if the letter "i" will execute the break command, if another letter "i" prints to the screen. Running the above code we get the result that the previous letters "i" have been printed. Then the loop ends, as shown below:

 str Kết thúc! 

Example 2:

 bien = 10 while bien > 0 : print ( 'Giá trị biến hiện tại là: ' , bien) bien = bien - 1 if bien == 5 : break print ( "OK!") 

The above code checks and prints the variable in descending value from 10, until the variable is equal to 5, the loop ends.

 Giá trị biến hiện tại là: 10 
Giá trị biến hiện tại là: 9
Giá trị biến hiện tại là: 8
Giá trị biến hiện tại là: 7
Giá trị biến hiện tại là: 6

OK!

The continue command in Python

The continue command is used to ignore the rest of the code inside the loop, applied to the current iteration. That means the loop does not terminate, it will continue with the next iteration.

Structure of continue:

 continue 

Diagram of the continue command in Python:

Break and continue commands in Python Picture 2

The continue statement in the for loop will look like this:

 for var in sequence: 

#khối code bên trong vòng lặp for

if dieu_kien:

continue

#code khác bên trong vòng lặp for

#code bên ngoài vòng lặp for

When the continue is executed, "#code inside the for loop" is ignored and returned to "# Block of code inside for for loop"

The continue statement in the while loop will look like this:

 while dieu_kien_kiem_tra: 

#code bên trong vòng lặp while

if dieu_kien:

tiếp tục

#code khác bên trong vòng lặp while

#code bên ngoài vòng lặp while

When the continue executing " #code khác bên trong vòng lặp while" will be ignored and returned to " #code bên trong vòng lặp while"

For example, the continnue command in Python

Example 3:

 # Sử dụng continue trong for 

for val in "string":
if val == "i":
continue
print(val)

print("Kết thúc!")

This code is identical to the above, only replace the break command with continue. Here, when looping the string "string" to the letter "i" will ignore the print variable print (val) and return to the command if val == "i" :, we get the result:

 strng Kết thúc! 

Example 4:

 bien = 10 
while bien > 0 :  
bien = bien - 1
if bien == 5 :
continue
print ( 'Giá trị biến hiện tại là: ' , bien)
print ( "OK!")

If bien = 5 then skip and repeat the next iteration, resulting in:

Giá trị biến hiện tại là: 9
Giá trị biến hiện tại là: 8
Giá trị biến hiện tại là: 7
Giá trị biến hiện tại là: 6
Giá trị biến hiện tại là: 4
Giá trị biến hiện tại là: 3
Giá trị biến hiện tại là: 2
Giá trị biến hiện tại là: 1
Giá trị biến hiện tại là: 0

OK!

In the next section we will learn about pass commands and iterative techniques in Python, so you can watch!

Exercises: More than 100 Python exercises have solutions (sample code)

Next article: Pass command in python

Previous article: while loop in Python

4 ★ | 1 Vote

May be interested

  • Multiple choice quiz about Python - Part 3Multiple choice quiz about Python - Part 3
    today's topic quantrimang wants to challenge you is about file and exception handling in python. let's try the following 15 questions!
  • 5 choose the best Python IDE for you5 choose the best Python IDE for you
    in order to learn well python, it is essential that you find yourself an appropriate ide to develop. quantrimang would like to introduce some of the best environments to help improve your productivity.
  • CONTINUE command in SQL ServerCONTINUE command in SQL Server
    the continue command is used to give command execution authority to the last expression of the loop. that means upside-down to the top of the loop, all the commands that follow in the loop containing continue will be ignored without execution.
  • What is Python? Why choose Python?What is Python?  Why choose Python?
    python is a powerful, high-level, object-oriented programming language, created by guido van rossum. python is easy to learn and emerging as one of the best introductory programming languages ​​for people who are first exposed to programming languages.
  • Module time in PythonModule time in Python
    python has a time module used to handle time-related tasks. tipsmake.com will work with you to find out the details and functions related to the time specified in this module. let's follow it!
  • Python data type: string, number, list, tuple, set and dictionaryPython data type: string, number, list, tuple, set and dictionary
    in this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
  • How to install Python on Windows, macOS, LinuxHow to install Python on Windows, macOS, Linux
    to get started with python, you first need to install python on the computer you are using, be it windows, macos or linux. below is a guide to installing python on your computer, specific to each operating system.
  • How to set up Python to program on WSLHow to set up Python to program on WSL
    get started with cross-platform python programming by setting up python on the windows subsystem for linux. here's how to set up python for wsl programming.
  • How to use Closure in PythonHow to use Closure in Python
    in this article, tipsmake.com will work with you to learn about closure in python, how to define a closure and why you should use it. let's go find the answer!
  • Summary of the common Run CMD commandsSummary of the common Run CMD commands
    instead of performing manual and direct access operations on windows, we can replace existing cmd commands for faster access.