Explanation of the control statement break in Python
Sentence concept break in Python
The sentence break is generally used to stop a loop if a certain condition is met. Then it moves to the next code in the program.
The concept of break statement in Python The break statement is generally used to stop the loop if a certain condition is met. Then you move to the next code in the program.
This sentence consists of one command and is written on a single line.
Examples of how to use the control syntax break in Python
In the following example, we have defined a loop while that would have printed all numbers from 1 to 10 had we not used the syntax break to make the loop stop when the counter counter becomes 5 .
In the following example we defined a while loop that would have printed all numbers from 1 to 10 had we not used the break statement to make the loop stop when the counter value becomes 5.
The first example in the sentence break
counter = 1
while counter <= 10:
print(counter)
if counter == 5:
break
counter += 1
print('The loop was stopped when counter =', counter)
• We will get the following result when running.
2
3
4
5
The loop was stopped when counter = 5
• So the sentence break made the loop stop when the counter value counter became 5 .
In the following example, we have defined a loop for that would have printed all numbers from 1 to 10 had we not used the syntax break to make the loop stop when the value n became 5 .
In the following example we defined a for loop that would have printed all the numbers from 1 to 10 were we not using the break statement to make the loop stop when the value of n becomes 5.
The second example in the sentence break
for n in range(1, 11):
print(n)
if n == 5:
break
print('The loop was stopped when n =', n)
• We will get the following result when running.
2
3
4
5
The loop was stopped when n = 5
• So the sentence break made the loop stop when the value of the variable became n equal to 5 .
Explanation of the control statement continue in Python
The sentence continue is generally used to stop the current cycle in a loop and move to the next cycle if a certain condition is met.
The concept of the statement to continue in Python The continue statement is generally used to pause the current session in a loop and move to the next cycle in it if a certain condition is met.
How to use syntax continue in Python
This sentence consists of one command and is written on a single line.
Examples of how to use the control syntax continue in Python
In the following example, we have defined a loop for that would have printed all numbers from 1 to 5 had we not used the syntax continue to make the loop go to the next cycle when the value n becomes 3 .
In the following example, we defined a for loop that would have printed all numbers from 1 to 5 if we hadn't used the continue statement to make the loop move to the next cycle when the value of n becomes 3.
The first example in the sentence continue
for n in range(1, 6): if n == 3: continue print(n)
• We will get the following result when running.
2
4
5
• So the sentence continue made the loop move to the next cycle when the value of the variable became n equal to 3 .
In the following example, we have defined a loop that prints all single numbers from 1 to 10 .
We use the syntax continue to make the loop pass every cycle in which the value of the variable n is a double.
In the following example, we defined a for loop that would have printed all numbers from 1 to 5 if we hadn't used the continue statement to make the loop move to the next cycle when the value of n becomes 3.
The second example in the sentence continue
for n in range(1, 11): if n %2 == 0: continue print(n)
• We will get the following result when running.
3
5
7
9
• So the sentence continue made the loop skip every cycle in which the value of the variable n was a single number.
In the following example, we have defined a loop that passes all the text characters stored in the variable sentence.
In this episode, we use the syntax continue to override each cycle in which the value of the variable n is equal to the letter a.
In the following example, we define a loop that passes all the text characters stored in the sentence variable. In this loop we used the continue statement to override every cycle in which the value of the variable n equals the letter a.
The third example in the sentence continue
sentence = 'harmash' for c in sentence: if c == 'a': continue print(c)
• We will get the following result when running.
r
m
s
h
• So the sentence continue made the loop skip every cycle in which the value of the variable n was equal to the letter a .