Python loop control statements
In this tutorial in you’ll learn to Python loop control statements like break statements, continue, and pass. Python loop control statements are used to handling the flow of the loop simple example terminate the loop or skip some block.
Python loop control :
- break
- continue
- pass
break
break statement useful to terminate the infinite loop and resume execution to next statement.
Syntax
break
Example
years = [1991,1992,1993,1994,1995,1996,1997,1998,1999,2000]
for year in years:
if year == 1995:
break
print(year)
print("Bye")
Output:
1991
1992
1993
1994
Bye
continue
continue statement returns the control to the beginning of the loop. continue statement skips the block of code which executes after it and returns the control to the beginning of the loop.
Syntax
continue
Example
years = [1991,1992,1993,1994,1995,1996,1997,1998,1999,2000]
for year in years:
if year == 1995:
continue
print(year)
print("Bye")
Output:
1991
1992
1993
1994
1996
1997
1998
1999
2000
Bye
pass
pass statement use to write empty loops.
Syntax
pass
Example
years = [1991,1992,1993,1994,1995,1996,1997,1998,1999,2000]
for year in years:
if year == 1995:
print ("pass statement")
pass
print(year)
print("Bye")
Output:
1991
1992
1993
1994
pass statement execute
1995
1996
1997
1998
1999
2000
Bye