Sunday, March 18, 2012

Conditional:Elif Statement

Hi In this post I will discuss about another Conditional Statement that is the Else If Statement.

In the last post, we have seen how the If-Else statement works, as A Quick Review, A certain block of codes under the If Statement is executed when the condition is true, and if the condition was false, it executed the block of codes under the Else Statement.

Here in this case, using the else-if statement, Python gets a list of choice to check for a condition, and if all the conditions in the choices are false, the codes under the else statement is executed.

Syntax:

if condition:
    #Executes this if the condition is true
elif condition:
    #Executes this if this condition is true and the above condition was false.
elif condition:
     #Executes this if this condition is true and the above condition was false.
else:
    #Executes this if all the condition above are false

Note: We can add as many elif statement as we want.

Lets check it out with a simple program, open up the Python Text Editor, and type:

p='food'
if p=='snake':
    print "Its a Snake"
elif p=='program':
    print "Its a Program"
else:
    print "I Dont know what it is"

Save it as 'anything.py' and then execute the program.

Output:

>>>
I Dont know what it is
>>>

Now if we change the value of 'p' to 'program':

p='program'
if p=='snake':
    print "Its a Snake"
elif p=='program':
    print "Its a Program"
else:
    print "I Dont know what it is"

The output we will get is:

>>>
Its a Program
>>>

Elif is pretty much self explanatory, there should not be any problem understanding it, if you have understood the earlier posts for If and If-Else.

Lets now make a Program, and check it out:

#Program to check the use of elif statement
ch=input("Enter 1 for add, 2 for Subtract, 3 for multiply: ")
if ch==1:
    a = input("Enter the First Number: ")
    b = input("Enter the Second Number: ")
    c = a+b
    print "The Answer is: ",c
elif ch==2:
    a = input("Enter the First Number: ")
    b = input("Enter the Second Number: ")
    c = a-b
    print "The Answer is: ",c
elif ch==3:
    a = input("Enter the First Number: ")
    b = input("Enter the Second Number: ")
    c = a*b
    print "The Answer is: ",c
else:
    print "Invalid Option"
raw_input("Press Enter To Exit")


Outputs:

>>> ================= RESTART ==================
>>>
Enter 1 for add, 2 for Subtract, 3 for multiply: 2
Enter the First Number: 20
Enter the Second Number: 10
The Answer is:  10
Press Enter To Exit
>>> ================= RESTART ==================
>>>
>>>
Enter 1 for add, 2 for Subtract, 3 for multiply: 1
Enter the First Number: 20
Enter the Second Number: 40
The Answer is:  60
Press Enter To Exit
>>> ================= RESTART ==================
>>>
Enter 1 for add, 2 for Subtract, 3 for multiply: 3
Enter the First Number: 50
Enter the Second Number: 5
The Answer is:  250
Press Enter To Exit
>>> ================= RESTART ==================
>>>
Enter 1 for add, 2 for Subtract, 3 for multiply: 4
Invalid Option
Press Enter To Exit
>>>

I Hope This wasn't difficult to understand, practice this, make own programs, to be a programmer, you have to be creative and make own programs.

Don't forget to check my next post.

Thank You.

0 comments:

Post a Comment