How to use if-else, elif decision control instruction in python with examples

By
There are two ways to control the program flow as given below:-
  1. Decision control instruction (e.g. if ,if-else,elif)
  2. Repetition control instruction (e.g. while,for)
Indentation in Python:-
  • Indentation is used to declare a block in python.If two statements are at the same indentation level,then they are the part of the same block.
  • Python doesn't allow the use of parentheses for the block of codes.
  • All the statements of one block are intended at the same level indentation.
  • In this,we will see how the actual indentation takes place in decision control instructions and others in python.
Decision control instruction:-
There are three ways for taking decisions in a program as given below.
  1. if condition
  2. if-else condition
  3. elif condition
1.) if condition:-This condition is used to test a specific condition in a program.If the condition true then if-block of codes will be executed.If the condition is false,no code will be executed.

Syntax:-
if condition:
 statement 1
 statement 2
 statement 3
a=int(input("Enter the number: \n"))
if(a%2==0):
    print("Number is even")
if(a>=0):
    print("Given number is positive number")
if(a<0):
    print("Given number is negative number")
print("This statement will always printed")
OUTPUT:-
2.) if-else condition:
This condition is also similar to the above if condition.If any program if condition i false,then else block of codes will be executed. 

Syntax:-
if condition:
 statement 1
 statement 2
else:
 statement 3
 statement 4
#Write a program to check whether number is even or odd.
n=int(input("Enter the Number: \n"))
if(n%2==0):
    print("Given number is even")
else:
    print("Given number is odd")
OUTPUT:-
#Write a program to check whether number is positive or Negative.
p=int(input("Enter the Number: \n"))
if(p>=0):
    print("Given number is positive")
else:
    print("Given number is negative")
OUTPUT:-
3.) elif condition:
This expression enables us to check multiple conditions and run the specific block of codes depending upon the conditions are true or false.We can use any number of elif condition depending upon our needs.The elif condition is optional.

Syntax:-
if condition:
 statement 1
 statement 2
elif condition 2:
 statement 3
 statement 4
elif condition 3:
 statement 5
 statement 6
else:
 statement 7
#Write a program to calculate the division obtained by the students.
percent=int(input("Enter the student percentage value :\n"))
if(percent>=60):
    print("You have passed first division")
elif(percent>40 and percent<=50):
    print("You have passed second division")
elif(percent>=33 and percent<=40):
    print("You have passed third division")
else:
   print("You have failed") 
OUTPUT:-
Nested if-else condition:-We can use nested if-else condition inside if block of codes.We will understand it with the help of an examples as given below.
#Write a program to check whether you are eligible for marriage or not according to government rules.
sex=input("Enter your Sex(M/F): \n")
age=int(input("Enter Your Age: \n"))
if(sex=='M'):
    if(age>=21):
        print("Your are eligible for marriage")
    else:
       print("Your are not eligible for marriage")
else:
     if(age>=18):
      print("Your are eligible for marriage")
     else:
      print("Your are not eligible for marriage")
OUTPUT:-
Note:-
  • : (colon) is compulsory after if,else and elif.
  • Condition is built using relation operators like <, >,<=,>=,==,!=
  • An if-else statement can be nested inside another if-else statement.
  • p=q is a assignment,while p==q is a comparison.
  • If a condition is true it is replaced by 1,if it false it is replaced by 0.
  • In if(p==q==r) result of p==q is compared with r.
  • Any non zero number is treated as true,0 is treated as false.
Watch complete Lecture video:-
---------coming soon------------------

0 comments:

Post a Comment

Powered by Blogger.