How to use Break in Python
In Python, break
statements can alter the flow of a normal loop.
Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking the test expression. The break
statements are used in these cases.
The break
statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If the break
statement is inside a nested loop, the break
statement will terminate the innermost loop.
# example # break s = "python" for i in s: if i == "o": break print(i, end = " ") print("-")
Output:
p y t h -
Subscribe
Login
Please login to comment
0 Discussion