Table of Contents
This guide covers break and continue commands in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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.
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:
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 forif 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 whileif 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 forfor val in "string":if val == "i":breakprint(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à: 10Giá tr? bi?n hi?n t?i là: 9Giá tr? bi?n hi?n t?i là: 8Giá tr? bi?n hi?n t?i là: 7Giá tr? bi?n hi?n t?i là: 6OK!
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:
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 forif 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 whileif 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 forfor val in "string":if val == "i":continueprint(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 = 10while bien > 0 :bien = bien - 1if bien == 5 :continueprint ( '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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.