Wednesday, March 21, 2012

Nested If Statement

In this Post, we will discuss about the Nested If Statement.

From the earlier post, we know that in an If-Else Statement, the statements inside the If body, executes only if the condition is true, otherwise it jumps to the Else statement or the Elif Statement.

Now Think Like This, what if we want the python to test a condition, and if the condition is True, we want Him to test another condition, and again if that's true, we want to test another...and it goes on. This is where we use Nested If Statements. In Other words, If Statements inside If Statements.

Lets check it out with a script, open up the Python Shell, and then the IDE by pressing Ctrl+n

Now lets type a small script to check this out.

foo1 = "python"
foo2 = "program"
if foo1 == "python":
    if foo2 == "program":
        print "Python is a Program"
    else:
        print "Maybe a Snake then"
else:
    print "I have no idea what it is then"

Save it as anything.py and execute the script, and it displays:

>>>
Python is a Program
>>>

Explanation:

Python knows that the value of foo1 is "python" and foo2 is "program", now when it comes to the if statement it checks the condition, and the condition is true, so python has to execute the the body of the if(Statements inside the If statement), but here again a testing condition, when tested the condition, the result is true, hence it has to execute the body of the If statement(The If Statement for which the condition was checked), since condition is true it prints out "Python is a Program"

Now lets change the script, and make the value of foo1 = "chocolate", and after executing, the display will be:

>>>
I have no idea what it is then
>>>

Now if we make the value of foo1 as "snake" and foo2 = "chocolate", the output will be:

>>>
Maybe a Snake then
>>>

[I Leave It To You, as why the output varied, Try to understand, but if you face any problem, leave a comment]

Now lets make a useful script:

# Program to check the usage of nested-if statement
nam = raw_input("Enter Your Name:  ")
marks = input("Enter Your Percentage:  ")
if marks < 100:
    print "\nHere is the comment for your percentage"
    if marks < 80:
       
        if marks < 60:
           
            if marks < 40:
               
                if marks < 30:
                    print "\nPoor! You Failed. "
            else:
                print "\nBad"
        else:
            print "\nGood"
    else:
        print "\nExcellent!!"
else:
    print "\nIdiot, Go and Do some Maths, percentage cannot be more than 100"
raw_input("\nPress Enter To Exit")

Outputs:

>>> ============= RESTART ================
>>>
Enter Your Name:  Whiskey
Enter Your Percentage:  200

Idiot, Go and Do some Maths, percentage cannot be more than 100

Press Enter To Exit
>>> ============= RESTART ================
>>>
Enter Your Name:  Whiskey
Enter Your Percentage:  25

Here is the comment for your percentage

Poor! You Failed.

Press Enter To Exit
>>> ============= RESTART ================
>>>
Enter Your Name:  Whiskey
Enter Your Percentage:  80

Here is the comment for your percentage

Excellent!!

Press Enter To Exit
>>>

Note:  We can see \n in the print statements...what is it?? \n means we are asking the Python to go to a new line.


Okay... I hope This should be clear with you, practice it, make your own programs, and Don't Forget to Check My next Post.

Thank You!

0 comments:

Post a Comment