Python Flow Control and Loop

Python Flow Control and Loop thumbnail
5K
By Dhiraj 15 March, 2019

Control flow statements are required to alter the flow of program execution based on the conditions. In this article, we will learn about different control flow statements available in Python programming language with examples. Below are the list of control flow statements that we will be discussing here.

  • Decision-making statements : if, else, elif
  • Looping statements : for, while
  • Branching statements : break, continue

If Statement in Python

If is the most basic control flow statement in Python. It enables the program to execute a certain part of the program if the given condition in an If statement is satisfied.

Syntax
if expression:
    ...body...

The program executes the statement only if the expression is evaluated to True.

As Python depends on indentation using whitespaces, all the control statements is terminated with colon indicating the body will be followed next and the first unindented line marks the end.

Example
a = 5
b = 10
if a < b:
    print("a is smaller than b")

The expression a < b is evaluated to True and hence the print statement is executed.

We can also use and, or conditions with if statements.

a = 5
b = 10
if a < b and a > 0:
    print("a is smaller than b but greater than 0")
    

else Statement in Python

As we discussed above, any expression evaluated to True leads the execution of it's body statements whereas else body statement is executed once the expression evaluates to false. It is an optional statement in if condtion.

Syntax
if expression:
    ...body...
else:
    ...body...
Example
a = 10
b = 5

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

Elif statement is used to chain multiple if statements. Once any one of the IF expression is evaluated to True, the program executes the body of it and exits the control flow, else the body of else is executed.

Syntax
if expression:
    ...body...
elif expression:
    ...body...
else: 
    ...body...
Example
a = 10
if a == 0:
    print("Zero")
elif a > 0:
    print("Positive number")
else:
    print("Negative number")

Output
Positive number

for Loop in Python

for loop is used to iterate over any iterable objects such as list, set, string etc. It visits each item in an iterable series unless the keyword break or continue is encountered and assigns it to a variablr that we specify in the for statement.

Syntax
for item in iterable:
	..body..
Iterating over Collection
fruits = ["apple", "orange", "grapes"]
for fruit in fruits:
    print(fruit)

Output
apple
orange
grapes
Iterating over Dict
person = {"name": "John", "age": 34, "city": "Delhi"}
for attr in person:
    print(attr, person.get(attr))

Output
name John
age 34
city Delhi

Iterating over String
person = "person"
for ch in person:
    print(ch)

Output
p
e
r
s
o
n

While Loop in Python

while loop in Python is referred with while keyword followed by an expression. The body of while loop is executed as long as the expression evaluates to True.

Synatx
while expression:
	...body...
Example

While loop executes untill a is greater than 1 as the expression evaluates to true.

a = 5
while a > 1:
    print(a)
    a -= 1

Output
5
4
3
2

break Statement in Python

The break keyword in Python terminates the innermost loop, transferring execution to the first statement after the loop. With the break statement we can stop the loop even if the while condition is true. For a nested loop, break terminates the innermost loop.

Example
a = 10
while a > 1:
    print(a)
    if a == 5:
        break
    a -= 1
	
Output
10
9
8
7
6
5

continue Statement in Python

Whenever a continue statement is encountered during a program execution, the execution flow skips the current iteration and goes to next iteration.

Example: The execution of program skips when the value of a becomes 3 and continues to next iteration.
a = 5
while a > 1:
    if a == 3:
        continue
    print(a)
    a -= 1
Output
5
4

Conclusion

In this article, we learnt about different control flow statements available in Python programming language such as if, else, elif, for, while, break, continue with examples.

Share

If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

author-image
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.

Further Reading on Python