Python

Python Control flow

Control flowExampleAnswerDescription
while loopx=0
while x<=3:
x=x+1
print(x)
1
2
3
4
while loopx=0
while x<=3:
x=x+1
if x==3:
continue
print(x)
1
2
4
Continue keyword continues with the next iteration. In this case 3 is not printed
while loopx=0
while x<=3:
x=x+1
if x==3:
break
print(x)
1
2
Break statement breaks the iteration and exits the loop. In this case after 1 and 2 are printed, the iteration stopped
for loopfor x in range(4):
x=x+1
print(x)
1
2
3
4
for loopfor x in range(4):
x=x+1
if x==3:
continue
print(x)
1
2
4
Continue keyword continues with the next iteration. In this case 3 is not printed
for loopfor x in range(4):
x=x+1
if x==3:
break
print(x)
1
2
Break statement breaks the iteration and exits the loop. In this case after 1 and 2 are printed, the iteration stopped
for loopfor x in range(5, 10, 2):
print(x)
5
7
9
Starting from 5 and until 10 increment by 2 and print
If statementx=8
if x>0 and x<=3:
print(“less than 4”)
elif x>3 and x<=7:
print(“less than 8”)
else:
print(“greater than 7”)
greater than 7