Saturday, March 17, 2012

Conditional:If Else Statement

The If - Else Statement

In this post we will learn another conditional statement, The If-Else Statement

From the Last Post, we learnt that, when we use the If statement, certain block of code will execute only if the condition is true, and would do nothing if the condition is false, simply the piece of codes under If statement is skipped if the condition was false.

Here, in If-Else Statement, It almost same, its just that when the condition is True, the block of codes under the If statement is executed, and if the condition is False the block of code under the Else Statement will be executed.

Syntax:

if condition:
    #codes here will be executed if the condition is true
else:
    #codes here will be executed if the condition in the if statement is false.

So lets check it out with a simple program, open up the Python text editor, and type:

p='program'
if p=='snake':
    print "Hey its a Snake!"
else:
    print "Hey its a Programming Language"
print "You have reached the last line of the program"
raw_input("Press Enter to Exit")

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

This is the Output For me:

>>>
Hey its a Programming Language
You have reached the last line of the program
Press Enter to Exit
>>>

So what happened exactly, when the program was executed,

1. From the first line Python understood that 'p' is a variable, and a String type Value 'program' is assigned to it.
2. It encounters the if statement, where it has to check if 'p' has the value 'snake', which is false, because value of 'p' is 'program', so the codes under the If statement is skipped.
3. Next it comes to the else statement, and python knows that he has to execute the block of codes under the else statement, as the if condition was false.
4. After executing the block of codes under the else statement, next it encounters the normal print statement and prints it.
5. The Last Line, If you don't know what it is, probably, u have not went through the earlier posts.

So when working with an If-Else just remember this algorithm.

A block of code is executed if the condition in the If Statement is true, and if its false, the block of code under the else statement is executed.


Now lets make a Program to Make it More Clear.

#Program to show the usage of if-else statement
pwd='password'
print "Welcome To Computer Korner"
raw_input("Enter Username Name: ")
test=raw_input("Enter Password: ")
if test==pwd:
    print "Password Accepted"
else:
    print "WARNING: Wrong Password"
raw_input("Press Enter")

Output:

>>>
Welcome To Computer Korner
Enter Username Name: Whiskey
Enter Password: abc123
WARNING: Wrong Password
Press Enter
>>> ================= RESTART ==================
>>>
Welcome To Computer Korner
Enter Username Name: Whiskey
Enter Password: password
Password Accepted
Press Enter
>>>


Okay So I Hope This Was Informative, practice it, Make your own programs and Don't Forget to check my next Post.


Thank You.

0 comments:

Post a Comment