Previous Index Next

Decision Structures

Sometimes the program needs to be executed depending upon a particular condition. Python provides the following statements for implementing the decision structure.

  • if statement
  • if else statement
  • if-elif-else Statement
  • nested if statement

if statement

In Python, the general format of the if statement:

if condition:
   statement
   statement
   etc.

If the condition is true, statement is executed; otherwise it is skipped

Program (absolute_number.py)

#This program illustrates if statement.

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

if number < 0:
    number =  - number
    
print("The absolute value of number is", number) 

Output

#1.
Enter a number:-20
The absolute value of number is 20
#2
Enter a number:100
The absolute value of number is 100

if

if-else Statement

The syntax looks like this:

if condition:
   statement 1
   etc.
else:
   statement 2
   etc.

The given condition is evaluated first. If the condition is true, statement1 is executed. If the condition is false, statement 2 is executed.

Program (even_odd.py)

# This program test a number is even or odd.

number = int(input('Enter number: '))

if number%2 == 0:
    print(number, 'is even.')
else:
    print(number, 'is odd.')

Output:

#1
Enter number: 15
15 is odd.
#2
Enter number: 20
20 is even.

if else

if-elif-else Statement

When there are more than two possibilities and we need more than two branches, if-elif-else statement is used. Here is the general format of the if-elif-else statement:

if condition_1:
   statement
   statement
   etc.
elif condition_2:
   statement
   statement
   etc.
else:
   statement
   statement
   etc.

elif is an abbreviation of "else if." Again, exactly one branch will be executed. There is no limit of the number of elif statements, but the last branch has to be an else statement.

Program (greater_number.py)

# This program determines greater number.

x = int(input("Enter first number:"))
y = int(input("Enter second number:"))

if x < y:
    print (x, "is less than", y)
elif x > y:
    print (x, "is greater than", y)
else:
    print (x, "and", y, "are equal")

Output

#1
Enter first number:25
Enter second number:36
25 is less than 36

#2
Enter first number:36
Enter second number:25
36 is greater than 25

#3
Enter first number:36
Enter second number:36
36 and 36 are equal

Nested Decision Structures

One conditional can also be nested within another. We could have written the above example as follows:

Program (nested_example.py)

# This program determines greater number.

x = int(input("Enter first number:"))
y = int(input("Enter second number:"))

if x == y:
    print (x, "and", y, "are equal")
else:
    if x < y:
        print (x, "is less than", y)
    else:
        print (x, "is greater than", y)

Output

#1
Enter first number:25
Enter second number:36
25 is less than 36

#2
Enter first number:36
Enter second number:25
36 is greater than 25

#3
Enter first number:36
Enter second number:36
36 and 36 are equal

Previous Index Next