Decision making statements – Python

  Decision making statements – Python

Fundamental


  Decision making statements


Decision making statements are used to execute code only when certain condition is satisfied. Python have decision making statements like if, f-else, elif.

If statement-

The if statement is used to execute statement(o) only if certain condition is satisfied. Following is the syntax for if statement:

If expression:

    statement(s)

The statement(s) will execute only if expression gives True and statement(s) will not execute if expression gives False.

In Python, the body of the if statement is indicated by the statement(s) with equal indentation. Python interprets non-zero values as True. None and 0 are interpreted as False.

 

Program for demonstration of if statement

n = int(input(“Enter a number:“))

if n <5:

print(n, “is less than 5“)

Output 1

Enter a number. 3

3 is less than 5

Output 2

Enter a number: 45

 

If-else statement

There else statement can be associated with if statement. It is used when you want to execute statement(s) on certain condition is satisfied and other statement(s) on that condition failure. Following is the syntax for if-else statement:

if expression:

statement(s)

else:

statement(s)

First expression after if will be solved if it gives True, then statement(s) of only if statement will be executed and if expression after if gives False, then statement(s) of only else statement will be executed.

 

Program for demonstration of if-else statement

n= int(input(“Enter a number: “))

if n <5:

print(n, “is less than 5“)

else:

print(n, “is not less than 5“)

Output 1-

Enter a number: 4

4 is less than 5

 

Output 2

Enter a number: 56

56 is not less than 5

 

Elif statements

There will be situations where you would like to execute only one group of statements from more than two group of statements, this can be achieved with the help of elif statement of python. Following is the syntax for elif statement:

if  expression:

  statement(s)

elif expression:

                     statement(s)

elif expression:

                     statement(s)

else:

          statement(s)

 

Program for demonstration of elif statement

n = int(“input Enter a number. “)

if  n<5

         print(n, “is less than 5″) 

elif n>5

         print(n, “is greater than 5“)

else:

        print(n, “Is equal to 5”)

 

Output 1

Enter a number: 2

2 is less than 5

 

Output 2

Enter a number: 7

7  greater than 5

 

Output 3

Enter a number:5

5 is equal to 5

Nested if-else statement-

There may be a situation where you will be required to test one condition after another one by one and this can be achieved using nested if else. Following is the syntax for nested if-else statement:

if expression:

               if expression:

                               statement(s)

              else:

                              statement(s)

else:

           if expression:

                            statement(s)

          else:

                            statement(s)

 

Program for demonstration of nested if-else statement

a = int(input(“Enter your age:“)

If a > = 1 and a < = 100 :

if a >= 18:

print (“Your are an adult“)

else:

print(n,”is not less than 5“)

else:

print(“invalid age entered“)

 

 Output 1

Enter your age: 23

Your are an adult

 

Output 2

Enter your age: 15

Your are not an adult

 

Output 3

Enter your age: -45

Invalid age entered

2 comments on “  Decision making statements – Python

Leave a Reply

Your email address will not be published. Required fields are marked *