Flow Control Statements in python:

In computer programming flow control statements are used to structure the program. These statements uses decision making, Looping and branching to structure the program. They basically determines the flow of program i.e In a program which line of code is going to be executed next is decided by these python flow control statements.

In python there are many flow control statements such as, if…elif…else, While, for loop, Break and continue etc. and many more which we are going to learn in our subsequent articles.In this article we are mainly going to focus on python decision making statements.

Decision Making in python:

Decision making is basically required where we want to test certain condition and the result decides the next block of code to be executed.

For decision making in python “If…elif…else” statements are used. In this article we are going to learn “If…elif…else” statement in detail.  So there are different types of if statement as below.

  1. if Statement
  2. if….else statement
  3. if….elif….else statement
  4. Nested if statements

Let us see each statement in detail;

Python if Statement:

Python “if” statement is first simple decision making statement. If statement consists of one logical test expression and indented line or block of code (also known as body of if statement or if block).

If statement checks the logical condition (Expression) provided and depending upon the result of tested condition it decides, whether to execute the “if block” or not.

If the test condition evaluates to “True” then it will execute the “if block” and if the test condition evaluates to “False” then it will skip the “if block” execution.

Indentation in python:

In other programming languages like java, c++ multiple lines of code is grouped using opening and closing parenthesis “{ }”. This grouping of the code in python is achieved using indentation. General indentation is four white spaces or a single tab. But again it completely depends on individual how much indentation he wants to keep. Click here to learn more about Indentation in python.

In our case we use indentation for writing the body of if statement or “if Block”. The body of if statement starts with first indented line of code and ends at first unintended line of code.

Python if statement syntax:

if Test Expression:
    Indented code lines (if block)

Here our python program will first evaluate the “Test Expression” and if the result comes out to “True” then it will execute the indented lines of code (if block).

If the test expression is evaluated to “False” then the code block execution will be skipped.

python flow control statements. if statement in python

Python logical test condition or expression:

  • Equal to: x == y
  • Not Equal to: x != y
  • Less than: x < y
  • Less than or equal to: x <= y
  • Greater than: x > y
  • Greater than or equal to: x >= y

Python if statement example:

Lets see few “If statement” examples in python:

#Example 1:
x = 10
y = 20
if x < y:
    print("x is less than y.")

Result: x is less than y.

#Example 2:
x = 10
y = 20
if x > y:
    print("x is greater than y")

Result:
Nothing will be printed since the test condition (x > y) is "False".

Python if…. else Statement:

Python if….else statement also tests the condition. It consists of one test expression and two code blocks. First code block is “if Block” and second code block is “else block”.

If the test expression evaluates to “True” then the “if block” is executed. If the expression evaluates to “False” then the “else block” is executed.

Python if statement syntax:

if Test Expression:
    Indented code lines (if block)
else:
    Indented code lines (else block)

python if else statement. python decision making statements

Python if….else statement example:

#Program to find greater number between x and y.
x = 10
y = 20
if x > y:
    print("x is greater than y.")
else:
    print("y is greater than x.")

#Result:
x is greater than y.

In above example we have tested the condition whether “x is greater than y”? It is found that y is greater than x. Hence if block is skipped and else block is executed.

Python if….elif….else Statement:

In above learned two statements i.e if and if….else statements we can test only one condition. Now using “if…elif….else” we can test multiple conditions. This statement consists of multiple test conditions and multiple code blocks.

elif is the combination of  “else and if”. It allows us to test multiple conditions.

Python if statement syntax:

if Test Expression: 
   Indented code lines (if block)
elif Test Expression:
   Indented code lines (elif block)
elif Test Expression:
   Indented code lines (elif block)
else Test Expression:
   Indented code lines (else block)

python flow control statements. if elif else statements in python

If the condition for “if” statement evaluates to “False” then it proceeds further and checks the next condition specified in “elif” statement. If all the conditions specified in “if” and “elif” statement evaluated to “False” then only body of “else” code is executed. We can have multiple elif statements to test multiple conditions.

Python if….elif….else statement example:

#Program to find greater number.
Test_Number = 385
if 9 < Test_Number < 99:
    print("Test_Number is a two digit number.")
elif 99 < Test_Number < 999:
    print("Test_Number is Three digit number.")
else:
    print("Test_Number is either a single digit or more than three digit number.")
#Result: 
Test_Number is Three digit number.

In above example Test_Number is tested in three different conditions.

  • First if condition (if 9 < Test_Number < 99) checks that, If the number lies between 9 to 99 it means the Test_Number is a two digit number.
  • Second elif condition (elif 99 < Test_Number < 999) executes when first if condition fails. If the Test_Number lies between 99 to 999 that means Test_Number is a three digit number.
  • Last else condition gets executed only after both if and elif condition fails. Since both the conditions are failed that means the Test_Number is either a single digit or more than three digit.

Python Nested if statements:

In python programming sometimes we may come across situations, where we want to check the second condition if the first condition evaluates to “True”.

Python if statement syntax:

if condition1:
   statement(s)
   if condition2:
      statement(s)
   elif condition3:
      statement(s)
   else:
      statement(s)
   elif condition4:
      statement(s)
else:
   statement(s)

If condition1 evaluates to “True” then the condition2 is further evaluated. In python we can nest any number of  if…elif…else statements inside if…elif….else statement.

Python Nested if….elif….else statement example:

Vehicle_Type = 'Four Wheeler'
#Vehicle_Type = 'Four Wheeler'
Total_Distance_Traveled = 320

if Vehicle_Type == 'Four Wheeler':
    if 1 < Total_Distance_Traveled < 250:
        print("Toll amount to be paid is 100.")
    elif 250 < Total_Distance_Traveled < 500:
        print("Toll amount to be paid is 200.")
if Vehicle_Type == 'Two Wheeler':
    if 1 < Total_Distance_Traveled < 250:
        print("Toll amount to be paid is 25.")
    elif 250 < Total_Distance_Traveled < 500:
        print("Toll amount to be paid is 50")

In above example, Road toll amount will be calculated depending upon type of vehicle and total distance the vehicle has traveled. So we have used nested if statements. First if statement is used to check the vehicle type and second if statement is checking total distance the vehicle has traveled.

Continue Learning…

Share this Post

Leave a Comment

Your email address will not be published.