Control flow statements are required to change the flow of program execution based on conditions. Today, we will learn about different control flow statements available in the Python programming language. The points mentioned below are the control flow declarations:
• Statements of decision-making: if, else, elif
• Statements of Looping: for, while
• Statements of Branching: break, continue
If Statement in Python
‘If’ is the most fundamental control flow statement in Python, the program to execute a certain part of the program if the condition given in an "If" statement is satisfied.
Syntax
if expression:
__body__
The statement will only be executed on the True evaluation of the expression.
Since Python depends on indentation using spaces, all control statements end with a colon indicating that the body will be followed later, and the first unindented line marks the end.
Example
a= 4
b= 8
if a < b :
print (“a is smaller than b”)
The expression a < b evaluates to True and the print statement is executed.
Moreover, the use of ‘and, or’ conditions can also be used with if statements.
a= 4
b=8
if a < b and a > 0 :
print (“a is less than b but more than 0”)
Statement ‘else’ in Python
As we discussed above, any expression evaluated to True leads to the execution of its body statements whereas another body statement, although a voluntary statement in if condition, is executed after the expression evaluates to false.
Syntax
if expression :
__body__
else :
__body__
Example
a= 8
b= 4
if a < b :
print (“a is smaller than b”)
else :
print (“a is greater than b”)
Output
a is greater than b
Elif statement in Python
The Elif statement is used to concatenate multiple if statements. After one of the ‘if’ expressions evaluates to True, the program executes the body and exits the control flow, otherwise, the body of else is executed.
Syntax
if expression :
__body__
elif expression:
__body__
else:
__body__
Example
a= 8
if a ==0 :
print (“Zero”)
elif a > 0 :
print (“Positive number”)
else :
print(“Negative number”)
Output
Positive number
for Loop in Python
The ‘for’ loop is used to iterate over any iterable object, such as a list, set, string, etc. It iterates through every element present in the chain until the break or continue keyword is experienced and assigns it to a variable that is defined in the "for" statement.
Syntax
for the element in variable :
__body__
Iterating over collection
vegetables = [“potato”, “peas”, “cabbage”]
for vegetable in vegetables :
print(vegetable)
Output
potato
peas
cabbage
Iterating over Dict
person = {“name”: “Ankit”, “age”: 28, “city”: “Delhi”}
for attr in person :
print(attr, person.get(attr))
Output
name Ankit
age 28
city Delhi
Iterating over String
animal = “tiger”
for ch in animal :
print(ch)
Output
t
i
g
e
r
While Loop in Python
The while loop in Python refers to the while keyword followed by an expression. The ‘while’ loop body is created until the True evaluation of the expression.
Syntax
‘while’ expression :
__body__
Example
a= 4
while a > 1
print(a)
a -= 1
Output
5
4
3
2
break Statement in Python
The break keyword in Python ends the innermost loop, passing execution to the first statement after the loop. The loop, even if the whole condition is true, can be stopped with the break statement. Break ends the innermost loop for an embedded loop.
Example
a= 8
while a > 1 ;
print(a)
if a ==3 :
break
a-= 1
Output
8
7
6
5
4
3
continue Statement in Python
Whenever a continue statement is encountered during program execution, the execution flow skips the current iteration and goes to the next iteration.
Example: The execution of the program jumps when the value of ‘a’ becomes 3 and continues to the subsequent iteration.
a= 5
while a > 1 :
if a == 3 :
continue
print(a)
a -= 1
Output
5
4
Build up your career with Python by enrolling for a Python Course in Noida at GICSEH.
Join GICSEH today!!