Friday, March 16, 2012

Conditional-If Statement

The If- Statement

In This Post I will show how Python tests a condition. From this post onwards I guess things would be more interesting.

Whenever we test something in program, we test it with a Conditional Statement

There are many Conditional Statements, our first basic conditional Statement will be the "If" Statement.

The If statement would check for a certain condition, and if the condition is true, it will execute certain piece of code.

Syntax:

if condition:
    #the codes in this block will execute if the condition is true

Notice something here, after the If statement, the next line has some space, this is known as Indention, indention makes python understand that it needs to execute the codes in the block, if the condition is true, if we don't have an indention, python will give an error.

Lets check with a basic program

Open up the IDLE, and then Open the Pythons Text Editor and type

python="programming"
if python=="programming":
    # "==" means a conditional operator, which tests, whereas "=" is an assignment operator, which assigns values to a variable
    print "Python is a programming Language"
print "Program executed successfully"
#The last print statement is not a part of If statement as its not indented

Note: You can skip the statements which starts with "#", statement starting with # is a comment line, and python does not care about it.

Now save program as anything.py, i have saved it as iftest.py, and run the program by pressing F5

Output:

>>>
Python is a programming Language
Program executed successfully
>>>

Now lets see what happens if the condition is not true.

python="Snake"
if python=="programming":
    # "==" means a conditional operator, which tests, whereas "=" is an assignment operator, which assigns values to a variable
    print "Python is a programming Language"
print "Program executed successfully"
#The last print statement is not a part of If statement as its not indented

The if statement will be skipped here, because, python is declared as Snake, and the if statement checks if python is a programming, which is not true

Output:

>>>
Program executed successfully
>>>

So we see here the block of "If" Statement did not run, because the condition did not meet.

Now lets see what if the condition is true, but we don't indent.

python="Snake"
if python=="Snake":
print "Python is a Snake"
#This time condition is True, but the the statement is not indented
print "Program executed successfully"
#The last print statement is not a part of If statement as its not indented

And try to run the program, the following error would appear:



Okay, So I believe by now The If statement is quite clear and also how to use it.

Now Lets make a small program using the If Statement.

# Program to check the use of IF Statement
print "Hi, Am I Python"
review = raw_input("What is your thoughts about Me, Do You Love me or Hate me? ")
test = review.lower()
if test=="love":
    print "I Love You Too"
print "Anyhow I dont care, If people Loves Me or Hates Me, I Love Myself, and Loves those who Loves me"
raw_input("Press Enter to exit")

Outputs:

>>>
Hi, Am I Python
What is your thoughts about Me, Do You Love me or Hate me? LOVE
I Love You Too
Anyhow I dont care, If people Loves Me or Hates Me, I Love Myself, and Loves those who Loves me
Press Enter to exit
>>>
================== RESTART ===================
>>>
Hi, Am I Python
What is your thoughts about Me, Do You Love me or Hate me? Love
I Love You Too
Anyhow I dont care, If people Loves Me or Hates Me, I Love Myself, and Loves those who Loves me
Press Enter to exit
>>>
================== RESTART ===================
>>>
Hi, Am I Python
What is your thoughts about Me, Do You Love me or Hate me? HATE
Anyhow I dont care, If people Loves Me or Hates Me, I Love Myself, and Loves those who Loves me
Press Enter to exit
>>>
================== RESTART ===================
>>>
Hi, Am I Python
What is your thoughts about Me, Do You Love me or Hate me? HaTe
Anyhow I dont care, If people Loves Me or Hates Me, I Love Myself, and Loves those who Loves me
Press Enter to exit
>>>

This Was Fun, Isn't it??

Okay I hope now If Statement is clear with you, Practice This, Make Your own Programs. And Don't Forget to Check my Posts.

Thank You.

0 comments:

Post a Comment