Different Types of Control Flow Statement in Python

351

In general day-to-day programming, there come situations where we need to control the normal flow of the program and restrict some piece of code to work or not depending on some sort of situation. We need to write some decision-making statements to do this. These decision-making statements are called control flow.

The statements available for control flow in Python are –

  • If / if-else/ if-elif-else
  • Nested if-else
  • For
  • While

If / if-else/ if-else-elif

If –

This statement is used to select whether a piece of code is going to run, or it is to be skipped.if statement flow control in python

We will understand this better with the help of the code given below

n = 10
if n % 2 == 0:
print("n is an even number")

The third line of the code runs only if the condition inside the if statement is true.

Output –

n is an even number

If-else –

In this case, there are a couple of codes. First, a condition is checked. If it is true, then the code present inside the if block is run. And, if it is not true, then the other part of the code that is present inside the else block is run.

n = 5
if n % 2 == 0:
print("n is even")
else:
print("n is odd")

Output –

n is odd

If-elif-else:

This is used to conditionally execute a statement or a block of statements.if else statement in python

x = 15
y = 12
if x == y:
print("Both are Equal")
elif x > y:
print("x is greater than y")
else:
print("x is smaller than y")

Explanation –

At first, the if statement is evaluated. If it comes to be true, the second and third will not be checked. If it is false, then the second statement is evaluated. And further, if it’s true the third is not evaluated and otherwise evaluated. And finally, the third statement is evaluated and will run based on the true/false of the third statement.

Nested If-Else

In Nested If-Else, there is an if-else statement inside the if-else statement. The nested if-else is useful when we want to make a series of decisions.

We will understand it better with the help of the example given below –

num1 = int(input('Enter first number '))
num2 = int(input('Enter second number '))
if num1 >= num2:
    if num1 == num2:
        print(num1, 'and', num2, 'are equal')
    else:
        print(num1, 'is greater than', num2)
else:
    print(num1, 'is smaller than', num2)

 

Output –

Enter first number: 56
Enter second number: 15\
56 is greater than 15

 

For Loop in Python:

We can iterate through the sequence of statements as the many numbers of times we want until the condition we have set becomes false.

for loop flow control in python

Example –

for i in range(1, 11):
    print(i)

Output –

1
2
3
4
5
6
7
8
9
10

While Loop in Python

In the while loop, every time the condition inside the while loop’s argument is checked. If it is true, the block of the whole body is checked. And if it is false, the control jumps to the next line after the while block.while loop flow control in python

Example –

num = 10
sum = 0
i = 1
while i <= num:
sum = sum + i
i = i + 1
print("Sum of first 10 number is:", sum)

Output –

The Sum of the first 10 numbers is: 55

So, in this article, we have learned about the different control statements that can be used to control the workflow in a program. Many a time, decision-making is used in a program, which is done by these inbuilt keywords.

Attempt Free Python MCQ Test

Previous QuizInterface In Java
Next QuizPrinciples of Ecology By Dr. Manoj Singh, Kalinga University

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.