if_stament

=The IF...ELIF...ELSE Statement= Conditional constructs are used to incorporate decision making into programs. The result of this decision making determines the sequence in which a program will execute instructions. You can control the flow of a program by using conditional constructs. This tutorial will discuss the programming conditional constructs available in Python, such as if, if...else, elif, and nested if. = The //if// statement: = The **if** statement of Python is similar to that of other languages. The **if** statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison. The syntax of the if statement is: code if expression: statement(s) code || Here **if** statement, condition is evaluated first. If condition is //true// that is, if its value is nonzero then the statement(s) block are executed. Otherwise, the next statement following the statement(s) block is executed. **Note:** In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.

Example:
code #!/usr/bin/python

var1 = 100 if var1: print "1 - Got a true expression value" print var1

var2 = 0 if var2: print "2 - Got a true expression value" print var2 print "Good bye!" code || This will produce following result: code 1 - Got a true expression value 100 Good bye! code || = The //else// Statement: = An **else** statement can be combined with an **if** statement. An **else** statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The //else// statement is an optional statement and there could be at most only one **else** statement following **if**. The syntax of the //if...else// statement is: code if expression: statement(s) else: statement(s) code ||

Example:
code #!/usr/bin/python

var1 = 100 if var1: print "1 - Got a true expression value" print var1 else: print "1 - Got a false expression value" print var1

var2 = 0 if var2: print "2 - Got a true expression value" print var2 else: print "2 - Got a false expression value" print var2

print "Good bye!" code || This will produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">1 - Got a true expression value 100 2 - Got a false expression value 0 Good bye! code || = The //elif// Statement = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The **elif** statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Like the **else**, the **elif** statement is optional. However, unlike **else**, for which there can be at most one statement, there can be an arbitrary number of **elif** statements following an **if**. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The syntax of the //if...elif// statement is: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s) code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">**Note:** Python does not currently support switch or case statements as in other languages.

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

var = 100 if var == 200: print "1 - Got a true expression value" print var elif var == 150: print "2 - Got a true expression value" print var2 elif var == 100: print "3 - Got a true expression value" print var else: print "4 - Got a false expression value" print var

print "Good bye!" code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This will produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">3 - Got a true expression value 100 Good bye! code || = The Nested //if...elif...else// Construct = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested **if** construct. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">In a nested **if** construct, you can have an **if...elif...else** construct inside another **if...elif...else**construct. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The syntax of the nested //if...elif...else// construct may be: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">if expression1: statement(s) **if expression2: statement(s) elif expression3: statement(s) else statement(s)** elif expression4: statement(s) else: statement(s) code ||

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

var = 100 if var < 200: **print "Expression value is less than 200" if var == 150: print "Which is 150" elif var == 100: print "Which is 100" elif var == 50: print "Which is 50"** elif var < 50: print "Expression value is less than 50" else: print "Could not find true expression"

print "Good bye!" code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This will produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Expression value is less than 200 Which is 100 Good bye! code || = Single Statement Suites: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If the suite of an **if** clause consists only of a single line, it may go on the same line as the header statement: <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is an example of a one-line if clause: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">if ( expression == 1 ) : print "Value of expression is 1" code ||