Decision Statements in Python

Decision Statements in Python

Let the program think 🧠

·

3 min read

Namaste Coders!

In this article, we will learn about decision/control statements in python that allows to control the flow of the program.

As in life we have to make various decisions according to the situation. In a similar manner our program needs some decision statements to control the flow of execution. These statements will decide what action to perform based on the condition. Decision making statements available in python are 👇

  • If statement
  • if else pair
  • elif statement
  • Nested decision statement

If Statement

if block statement is the simplest decision statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. Below is the flow chart which explains the execution 👇

46.PNG

Example

# TODO: WAP to find whether you can drive or not
age = 19

if age >= 18:
    print("You can drive!")
else:
    print("You cannot drive!")

#output:
#You can drive!
# TODO: WAP to find whether a given num is even

num = 24

if num % 2 == 0:
     print("Num is even")


#Output: 
#Num is even

if else pair

This is an extended version of the if statement. In it if and else block will work in pair. When in any case if condition doesn’t satisfy i.e., it is not True then else block statement(s) is/ are executed. You can understand it better with the help of the flowchart shown below 👇

47.PNG

# TODO: WAP to find whether a given num is even or odd

# num = int(input("Enter the num : "))

# if num % 2 == 0:
#     print("Num is even")
# else:
#     print("Num is odd")

When we provide 4 as input then if the block statement executes as it satisfies the condition (num%2==0 i.e. 4%2==0) whereas when we provide 5 as input then it doesn’t satisfy if the condition so else block statement is executed.

if elif Statement

It allows you to check multiple expression blocks, unlike else statements. For instance, we need to check more than one condition and execute blocks according to that. Flowchart is provided for better understanding 👇

48.PNG

Example

# TODO: Grading system
# marks = float(input("Enter the marks: "))

# if marks > 90 and marks <= 100:
#     print("GRADE : A+")
# elif marks > 80 :
#     print("GRADE : A")
# elif marks >70:
#     print("GRADE: B+")
# elif marks > 60:
#     print("GRADE : C")
# elif marks > 50:
#     print("GRADE : D")
# elif marks >40:
#     print("GRADE : E")
# elif marks <=40 and marks>=0:
#     print("GRADE : F")
# else:
#     print("Enter a valid number")

Output: Enter your marks: 50 Grade : D

Enter your marks: 60 Grade : C

Enter your marks: 25 Grade : F

It checks each expression block until any one condition satisfies, otherwise at last else condition is executed when no elif condition is True.

Nested if - else statements

Nested if-else statements facilitate us to check conditions by using if-else statement inside another if statement. For example, if we need to check whether the number inserted is divisible by 3. If this condition becomes true then only we can check that the inserted number is 3 or not. Check the code for this.

num = int(input("Enter a num: "))

if num % 3 == 0:
    if num == 3:
        print("It is number 3")
    else:
        print("It is a factor of 3")
else:
    print("It is not divisible by 3")

Output: Enter a number: 3 It is 3

Enter a number: 45 It's one of factor is 3

Enter a number: 14 It is not divisible by 3

The End

I hope you enjoyed the article and had a good learning experience.

Follow for more articles and keep sharing👩

Keep coding

Linkedin

hashnode blogs

Did you find this article valuable?

Support akshita garg by becoming a sponsor. Any amount is appreciated!

Â