Python Loops
Python has two primitive loop commands:
while loops
for loops
Loop Flow diagram
while loop Syntax
while expression:
statements
For Loop Syntax
for var in iterable:
statements
Q) Write a python program to print 1 to 10.
print(“1”)
print(“2”)
print(“3”)
print(“4”)
Print(“5”)
print(“6”)
print(“7”)
print(“8”)
print(“9”)
Print(“10”)
Using while loop –
n=1
while n <= 10:
print(number)
n=n+1
Using For loop –
for n in range(10):
print(n)
Q) Write a program to print all the odd number till 100.
Python break and continue
In programming, the break and continue statements are used to alter the
flow of loops:
break exits the loop entirely
continue skips the current iteration and proceeds to the next one
Working of Python break Statement
Example –
for i in range(5):
if i == 3:
break
print(i)
Output -
0
1
2
Working of continue Statement in Python
Example –
for i in range(5):
if i == 3:
continue
print(i)
Output-
0
1
2
4