Decision making statement in python

Decision making statement in python


In this statement we can take a decision for executing block of statement.if in this case condition is checked.if condition is true then the true part will execute,otherwise false part will execute.
     
 There are four types of decision making statement.

(1) simple if statement/single alternative if statement.
(2) if-else statement/double alternative if statement.
(3) if-else-if statement/else if ladder statement.
(4) Nested if statement/compound if statement.


(1)simple if statement/single alternative if statement
 In this statement if condition is true then true part will execute otherwise no action will generate.
 Syntax:
if  test expression:
    Statement 1
    Statement 2
    Statement 3
   ---------------
   ---------------
    Statement n
Statement x

Examples:

if ( age>=18) :
   print("You are eligible for vote")



(2) if-else statement/double alternative if statement
In this statement if condition is true then true part will execute otherwise false part will execute.

Syntax:

if  test expression:
    Statement 1
    Statement 2
    Statement 3
   ---------------
   ---------------
    Statement n
else:
    Statement 1
    Statement 2
    Statement 3
   ---------------
   ---------------
    Statement n
Statement x

example:

if(age>=18) :
   print("you are eligible for vote")
else:
  print("You are not eligible for vote")


(3)if-elif-if Statement
 It is generally used specify more than one condition, in this case if condition is true then true 
part will execute after that terminate from if statement otherwise next condition is checked.  

Syntax:
if (test expression 1):
    block of statement 1

elif (test expression 2):
    block of statement 2
--------------------------
elif (test expression n):
    block of statement n
else:
   block of statement x
Statement y




(4) Nested if statement/compound if statement 
A statement that contains  other statement is called a compound statement.
When one if statement is defined within another if statement called Nested if. In this case one is called outer if & another is called inner if.





Exercise:
1.Decision making statement in python


Q: write a program in python to find the greatest number between three number.


num1= input("Enter first number :")
num2= input("Enter Second number :")
num3= input("Enter third number :")
if(num1>num2):
    if(num1>num3):
        print(num1, "is greater")
    else:
        print(num3, "id greater")    
elif(num2> num3):
    print(num2,"is greater")
else:
    print(num3, "is greater")    


Q:write a program in python in which user input day number(1 to 7) after that print day.


day= int(input("Enter number (1 - 7) :"))
if(day==1):
    print("monday")
elif(day==2):
    print("tuesday")
elif(day==3):
    print("Wednesday")
elif(day==4):
    print("thrusday"
elif(day==5):
    print("friday"
elif(day==6):
    print("Saturday")      
elif(day==7):
    print("Sunday")     
else:
    print("invalid input !")           

Q: write a program in python  to find roots of quadratic equation.


a= int(input("enter the value of a :"))
b= int(input("enter the value of b :"))
c= int(input("enter the value of c :"))
d=(b*b)-(4*a*c)
deno=2*a
if(d>0):
    print("REAL ROOT")
    root1=(-b + d**0.5)/deno
    root2=(-b - d**0.5)/deno
    print("Root 1 :",root1," Root 2 :", root2)
elif(d==0):    
     print("EQUAL ROOT")
     root1=-b/deno
     print("Root 1 :",root1," Root 2 :", root1)  
else:
    print("imaginary root")        

Thank you..........
please like and follow my Facebook page.....
Let's_code

Lopping_statement_in_python
function_in_python
Pattern program 

Comments

Popular posts from this blog

python pattern programming

javaScript Calculator.