Reference site: [Introduction to Python] How to stop a loop using break?
There are two types of Python loop statements, a for statement and a while statement. The for statement repeats the process a certain number of times, and the while statement repeats the process while satisfying specific conditions. It's a convenient syntax that repeats the same process without having to enter it over and over, but sometimes you want to get out of the loop statement in the middle. In such a case, you can use the break statement to interrupt the process and get out of the loop statement. This time, I will explain how to use the break statement.
If you want to end the loop processing in the middle, you can use the if statement to do it in a pseudo manner. As an example, suppose you want to examine the elements of a loop and write code that will result in an error if there are numbers greater than 100.
list1 = [24, 55, 32, 65, 74, 120, 72, 67]
loop_flag = 0
for num in list1:
if(loop_flag == 0):
if(num < 100):
print(num)
else:
loop_flag = 1
if(loop_flag == 1):
print('Error: Invalid number found')
Execution result
24 55 32 65 74 Error: Invalid number found
In this example, a flag (loop_flag) for whether to continue the loop is prepared, and when a number that satisfies the condition appears, the flag is set to 1 so that the processing inside the loop is not executed. This method seems to break the loop at first glance, but in reality it is very wasteful and the code is long just to do nothing in the middle. In such a case, use a break statement.
list1 = [24, 55, 32, 65, 74, 120, 72, 67]
for num in list1:
if(num >= 100):
print('Error: Invalid number found')
break
print(num)
Execution result
24 55 32 65 74 Error: Invalid number found
The execution result is exactly the same as before, but using break is shorter and easier to see. The break statement is mainly used with the if statement in the loop statement. If a specific condition is met in the if statement, the break statement is used to break the loop in the middle and break out. All the syntax after the break statement is skipped, so when displaying an error statement etc., write it before the break statement.
It is a break statement that breaks the loop in the middle, but of course it can also be used in a while statement.
list1 = [24, 55, 32, 65, 74, 120, 72, 67]
index = 0
while(index < len(list1)):
if(list1[index] >= 100):
print('Error: Invalid number found')
break
print(list1[index])
index += 1
Execution result
24 55 32 65 74 Error: Invalid number found
I replaced the for statement with a while statement, but the usage of the break statement is the same. Of course, the result will be the same.
Like the if statement, Python's for and while statements have else clauses. The else clause of the loop statement is executed if the iteration ends successfully. For example, if you want to display a message telling you that the loop statement has ended after you have cycled through the loop statement, you would do the following:
list1 = [24, 55, 32, 65, 74, 120, 72, 67]
index = 0
for num in list1:
print(num)
else:
print('The loop is complete.')
Execution result
24 55 32 65 74 120 72 67 The loop is complete.
It is a very convenient else clause because it can be processed only once in the same loop block, but it is not actually executed when the break statement breaks the loop.
list1 = [24, 55, 32, 65, 74, 120, 72, 67]
index = 0
for num in list1:
if(num >= 100):
print('Error: Invalid number found')
break
print(num)
else:
print('The loop is complete.')
Execution result
24 55 32 65 74 Error: Invalid number found
As in the previous examples, if a number of 100 or more is found, an error will occur and the loop will be broken. Then, the last else clause is not executed and the message "Loop completed" is not displayed.
If you use break and else, you can easily "determine whether or not you have broken and perform a specific process only when it is not."
Normally, prepare a variable to determine whether it has broken, and check the contents of the variable with an if statement to determine whether it has broken. It's a hassle because it increases useless variables. If it is an else clause, you only have to write the processing to be performed only when there is no break in it. You don't need useless variables or if statements.
Recommended Posts