Sunday, May 19, 2013

Python onditional Statements -If structue

Conditionals are where a section of code is only run if certain conditions are met. This is similar to the 'while' loop you just wrote, which only runs when x doesn't equal 0. However, Conditionals are only run once. The most common conditional in any program language, is the 'if' statement. Here is how it works:
Code Example 6 - if statement and example
'''
if {conditions to be met}:
    {do this}
    {and this}
    {and this}
{but this happens regardless}
{because it isn't indented}
'''
 
#EXAMPLE 1
y = 1
if y == 1:
    print "y still equals 1, I was just checking"
 
#EXAMPLE 2
print "We will show the even numbers up to 20"
n = 1
while n <= 20:
    n = n + 1
    if n % 2 == 0:    
           print n
print "there, done."
Example 2 there looks tricky. But all we have done is run an 'if' statement every time the 'while' loop runs. Remember that the % just means the remainder from a division - just checking that there is nothing left over if the number is divided by two - showing it is even. If it is even, it prints what 'n' is.

'else' and 'elif' - When it Ain't True [edit]

There are many ways you can use the 'if' statement, to deal with situations where your boolean expression ends up FALSE. They are 'else' and 'elif'.
'else' simply tells the computer what to do if the conditions of 'if' aren't met. For example, read the following:
Code Example 7 - the else statement
a = 1
if a > 5:
    print "This shouldn't happen."
else:
    print "This should happen."
'a' is not greater than five, therefore what is under 'else' is done.
'elif' is just a shortened way of saying 'else if'. When the 'if' statement fails to be true, 'elif' will do what is under it IF the conditions are met. For example:
Code Example 8 - The elif statement
z = 4
if z > 70:
    print "Something is very wrong"
elif z < 7:
    print "This is normal"
The 'if' statement, along with 'else' and 'elif' follow this form:
Code Example 9 - the complete if syntax
if {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
elif {conditions}:
    {run this code}
else:
    {run this code}

#You can have as many or as few elif statements as you need
#anywhere from zero to the sky.
#You can have at most one else statement
#and only after all other ifs and elifs.
One of the most important points to remember is that you MUST have a colon (:) at the end of every line with an 'if', 'elif', 'else' or 'while' in it. I forgot that, and as a result a stack of people got stumped at this lesson (sorry ;)).

Indentation [edit]

One other point is that the code to be executed if the conditions are met, MUST BE INDENTED. That means that if you want to loop the next five lines with a 'while' loop, you must put a set number of spaces at the beginning of each of the next five lines. This is good programming practice in any language, but python requires that you do it. Here is an example of both of the above points:
Code Example 10 - Indentation
a = 10
while a > 0:
    print a
    if a > 5:
        print "Big number!"
    elif a % 2 != 0:
        print "This is an odd number"
        print "It isn't greater than five, either"
    else:
        print "this number isn't greater than 5"
        print "nor is it odd"
        print "feeling special?"
    a = a - 1
    print "we just made 'a' one less than what it was!"
    print "and unless a is not greater than 0, we'll do the loop again."
print "well, it seems as if 'a' is now no bigger than 0!"
print "the loop is now over, and without furthur ado, so is this program!"
Notice the three levels of indents there:
Each line in the first level starts with no spaces. It is the main program, and will always execute. Each line in the second level starts with four spaces. When there is an 'if' or loop on the first level, everything on the second level after that will be looped/'ifed', until a new line starts back on the first level again. Each line in the third level starts with eight spaces.

WikiMedia

No comments:

Post a Comment