Control Statements In Python

Python control statements, like break and continue, are used to change the way loops run by default. They let you change a loop’s behaviour according to certain criteria. To repeat a series of assertions, loops are necessary.
Based on the following explains the break and continue statements:
Break Statement
In a while or for loop, the break statement is used to end the current loop instantly. The program quits the loop and resumes execution at the first statement just after the loop body when it comes across a break statement. It exits the nearest enclosing loop abruptly.
When a condition is satisfied within the body of the loop typically verified by an if statement the break statement is frequently utilized. By ending the loop when additional execution is not required, the use of breaks can occasionally streamline code and increase productivity.
Unless the innermost loop is nested, a break statement will not finish it.
Break can be useful, but excessive use might make programs harder to debug and explain. Any loop can be written without a break, usually by utilizing a flag variable or changing the loop’s condition. An Example of Code Making use of breaks.
Example :
# Example using break
string val = "Hello World"
print("Processing string using break:")
for val in string val: # Using 'val'
if val == " ":
print("Space encountered!")
break # Exits the for loop immediately
print(val)
print("Finished.") # This line is outside the loop
Output:
Processing string using break:
H
e
l
l
o
Space encountered!
Finished.
Because the if val == ” “: condition becomes true when the space is reached and the break statement is executed, the loop in this output ends after writing “o”. The loop does not analyse the remaining characters (‘W’, ‘o’, ‘r’, ‘l’, and ‘d’). After the loop, the execution leaps to the print(“Finished.”) declaration.
Continue Statement
For and while loops also make use of the continue statement. The continue statement does not end the loop in its entirety, in contrast to break. For the current iteration only, it bypasses the remaining code inside the loop.
The software jumps back to the top of the nearest enclosing loop when continue is executed. After reevaluating the loop’s condition or expression, if the condition is still true, the loop moves on to the following iteration. This enables you to bypass particular values or conditions inside a loop.
Similar to break, continue is usually used in an if test and can enhance a loop’s readability and control flow.
Like break, continue should be used rarely because it can make it more difficult to follow a loop’s flow. It is also possible to write any loop without continue.
Example:
# Example using continue
print("Processing string using continue:")
for val in "string": # Using 'val'
if val == "i":
print("Skipping 'i'")
continue # Skips the rest of the loop body for this iteration
print(val)
print("Finished.")
output:
Processing string using continue:
s
t
r
Skipping 'i'
n
g
Finished.
Upon reaching the character ‘i’ in this output, the loop executes the continue statement and the if val == “i”: condition is true. This results in the skip of the print(val) statement for that particular iteration. After that, the loop instantly advances to the following character (‘n’) and carries on as usual. Until every character in the string has been handled, the loop does not completely stop. Both break and continue are effective methods for changing how loops are executed depending on the circumstances that arise during iteration.