Flow Control
The statements in your code are generally executed from top to bottom, in the order that they
appear. Control flow statements, however, break up the flow of execution by employing decision
making, looping, and branching, enabling your program to conditionally execute particular blocks
of code. We've 3 types of flow control statements -
Conditional/decision-making - Gives your code some decision making abilities, We can
control what statements get executed depending on a condition.
Iterative/looping - Help us repeat a piece of code for some number of times.
Transfer - These statements help us terminate a looping/iterative statement or skip a
certain iteration depending on some condition.
We can use these statements individually, or even combine them with one another.
CONDITIONAL / DECISION-MAKING STATEMENTS :
if (condition) : if (condition) : if (condition_one) :
dothistask dothistask dothistask
else : elif (condition_two) :
dothattask dothattask
else :
defaulttalk
name = input('Your name : ') name = input('Your name : ') name = input('Your name : ')
if (name == 'Python'): if (name == 'Python'): if (name == 'Python'):
print('Hi, Python') print('Hi, Python') print('Hi, Python')
else : elif (name == 'Java'):
print('Hi, User') print('Hi, Java')
else :
print('Hi, User')
ITERATIVE/LOOPING STATEMENTS :
for
# To perform a task specific number of times # To iterate over an iterable and perform a
task on each of its value
for i in range(number) : for each in iterable :
dothistask dothistask
for i in range(1,5): names = ['python', 'java', 'JavaScript']
print(i) for name in names:
print(name)
while
# 'While' continues testing the condition & executing its block until condition evaluates to false.
while (condition) :
dothistask
# infinite loop # finite loop
x=1 x=1
while(x<=3): while(x<=3):
print(x) print(x)
x = x+1
#11111...
#123
TRANSFER STATEMENTS :
break continue
for i in range(1,10): for i in range(1,10):
if i==5: if i==5:
break continue
print(i) print(i)
#1234 #12346789