Thứ Năm, Tháng Ba 6, 2025
spot_img
HomeBreak and Continue commands in Python

Break and Continue commands in Python

What is Continue in Python? How is the Continue in Python and Break used? Let's find out together!

Python's flexibility makes it the best solution to automate and perform repetitive tasks effectively. In Python (and other programming languages), the loop helps repeat the list, set, chain, dictionary and gathering.

Regarding Loop, you will need to know both Continue and Break. Let's find out together!

In Pythoncommand break and continue Allows you to control the activity of a regular loop.

The usual loop will repeat a block of code until the test conditions False. However, there will be times when you want to stop the current loop or even the entire loop without checking the conditional expression anymore. That is when we need the help of the command break and continue.

Break command in python

Command break In Python used to finish the loop and transmit the control to the next statement of the loop. If the command break In a cage loop (the loop inside another loop), break will terminate the inside loop.

You can use the command break To get rid of the loop when a specific condition is met (True), so the command break Often accompanied by the command if.

The syntax of the Break command:

break

Break command diagram:

Break command diagram in python

If using the break in the For Python loop, it will be as follows:

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 executed “#code other inside the For loop“Will be overlooked and transferred”#code outside the loop for“.

If using the break in the While Python loop will be as follows:

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 executed “#code other inside the While loop“Will be overlooked and transferred”#code outside the While loop“.

Examples of the Break Python command

Example 1:

#Sử dụng break trong for

for val in "quantrimang":
    if val == "m":
        break
    print(val)

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

In the above code, we repeat the characters in the chain quantrimang and check the condition, if the letter is m will execute the command breakif the letter is different m Print on the screen. Running the code above, the result is the previous letters m has been printed. Then the loop ends, as the result below:

q
u
a
n
t
r
i
Kết thúc!

Example 2:

#Ví dụ sử dụng break trong while trên QuanTriMang:

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 variables according to the decreasing value from 10, until the variable is equal to 5, then finish the loop.

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!

Continue command in python

Command continue Used to skip the rest of the code inside the loop, applicable to the current repetition. Unlike the command breakstatement continue It will not completely stop the loop, but it simply jumps to the next loop.

Command continue Used in case you want to skip your loop at a definite condition, then continue to repeat.

Ordinance of Continue:

continue

Continue command diagram in Python:

Continue command diagram in python

The Continue command in the For loop will be as follows:

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 continue executed “#code other inside the For loop“Skiped and returned”#Code block inside the loop

The Continue command in the While loop will be as follows:

while dieu_kien_kiem_tra:
    #khối code bên trong vòng lặp while
    if dieu_kien:
         continue

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

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

When continue executed “#code other inside the While loop“Will be overlooked and come back”#Code block inside the While loop

The difference between Continue and Pass in Python:

  • Pass: Pass statement in Python is used when a must -have statement or condition must be in the program, but we do not want any commands or code to be executed. This statement is often used as a reservation for future code.
  • Continue: Continue statement in Python is used to skip the remaining code inside the loop only for the current repetition.
  • Break: The break statement in the python changes the circle of the loop by finishing the loop when a conditioned condition is met.

Example of Continnue in Python

Example 3:

# Sử dụng continue trong for

for val in "quantrimang":
    if val == "m":
       continue
    print(val)

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

This code is the same as above, just replace the command break equal continue. Here, when repeating the string “quantrimang“To the letter m will ignore the printing command print(val) and return to the loop whilewe have results:

q
u
a
n
t
r
i
a
n
g
Kết thúc!

Example 4:

# Ví dụ sử dụng continue trong while trên QuanTriMang:

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 ignore and perform the next repeat, the result is:

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!

Overall, the Break and Continue commands in Python are used to skip the current Loop and Break parts out of the loop completely.

  • Command break Can be used if you need to get out of the For or While loop and move to the next code.
  • Command continue Can be used if you need to skip the current transformer of for or while and move into the next loop.

The difference between break and Continue in python

The main difference between the break and the Continue in Python is the loop termination. The following table will show you the difference between break and Continue in Python.

Comparative criteria

Break

Continue

Mission

Eliminate the remaining loop.

Only end the current loop.

Activity after break/Continue

'Break' will continue to control the program until the end of the loop surrounding that 'break'.

'Continue' will continue to control the program for the next repetition of that loop with 'Continue'

Impact

End the loop early.

Make the next loop early.

Continuous

'break' stop continuing the loop

'Continue' does not stop continuing the loop and it stops the current loop.

Other factors

Used with 'switch', 'label'

Can not be executed with Switch and Label.

Overall, Continue and Break for Python have many differences but both are useful in programming. Therefore, you should definitely master these two functions when programming applications, software or any projects.

So in the above content, I introduced you how to use the command break and continue Attached with specific examples. In the following section we will learn about the password and repeat techniques in Python, please follow!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments