Sunday, May 19, 2013

Python While Loop - Repetition Structure


The following are examples of a type of loop, called the while loop:

Code Example 1 – The while loop
a = 0
while a < 10:
    a = a + 1
    print a
How does this program work? Let's go through it in English (this is called pseudocode):
Code Example 2 – Plain-language while loop
'a' now equals 0
As long as 'a' is less than 10, do the following:
    Make 'a' one larger than what it already is.
    print on-screen what 'a' is now worth.
What does this do? Let's go through what the computer would be 'thinking' when it is in the while loop:
Code Example 3 – while loop process
#JUST GLANCE OVER THIS QUICKLY
#(It looks fancy, but is really simple.)
Is 'a' less than 10? YES (it's 0)
Make 'a' one larger (now 1)
print on-screen what 'a' is (1)

Is 'a' less than 10? YES (it's 1)
Make 'a' one larger (now 2)
print on-screen what 'a' is (2)

Is 'a' less than 10? YES (it's 2)
Make 'a' one larger (now 3)
print on-screen what 'a' is (3)

Is 'a' less than 10? YES (it's 3)
Make 'a' one larger (now 4)
print on-screen what 'a' is (4)

Is 'a' less than 10? YES (it's 4)
Make 'a' one larger (now 5)
print on-screen what 'a' is (5)

Is 'a' less than 10? YES (it's 5)
Make 'a' one larger (now 6)
print on-screen what 'a' is (6)

Is 'a' less than 10? YES (it's 6)
Make 'a' one larger (now 7)
print on-screen what 'a' is (7)

Is 'a' less than 10? YES (are you still here?)
Make 'a' one larger (now 8)
print on-screen what 'a' is (8)

Is 'a' less than 10? YES (it's 8)
Make 'a' one larger (now 9)
print on-screen what 'a' is (9)

Is 'a' less than 10? YES (it's 9)
Make 'a' one larger (now 10)
print on-screen what 'a' is (10)

Is 'a' less than 10? NO (it's 10, therefore isn't less than 10)
Don't do the loop
There's no code left to do, so the program ends.
So in short, try to think of it that way when you write while loops. This is how you write them, by the way:
Code Example 4 – while loop form
while {condition that the loop continues}:
    {what to do in the loop}
    {have it indented, usually four spaces}
{the code here is not looped}
{because it isn't indented}
An example:
Code Example 5 – while loop example
#EXAMPLE
#Type this in and see what it does.
x = 10
while x != 0:
    print x
    x = x - 1
    print "Wow, we've counted x down, and now it equals", x
print "And now the loop has ended."
Remember, to make a program, you open IDLE, click 'File > New Window', type your program in the new window, then press F5 to run.

Boolean Expressions (Boolean... what?!?) [edit]

What do you type in the area marked {conditions that the loop continues}? The answer is a boolean expression.
What? A forgotten concept for the non-math people here. Never mind, boolean expression just means a question that can be answered with a TRUE or FALSE response. For example, if you wanted to say your age is the same as the person next to you, you would type:
My age == the age of the person next to me
And the statement would be TRUE. If you were younger than the person opposite, you'd say:
My age < the age of the person opposite me
And the statement would be TRUE. If, however, you were to say the following, and the person opposite of you was younger than you:
My age < the age of the person opposite me
The statement would be FALSE - the truth is that it is the other way around. This is how a loop thinks - if the expression is true, keep looping. If it is false, don't loop. With this in mind, lets have a look at the operators (symbols that represent an action) that are involved in boolean expressions:
Table 1 - Boolean operators
ExpressionFunction
<less than
<=less that or equal to
>greater than
>=greater than or equal to
!=not equal to
<>not equal to (alternate, != preferred)
==equal to
Dont get '=' and '==' mixed up - the '=' operator makes what is on the left equal to what is on the right. the '==' operator says whether the thing on the left is the same as what is on the right, and returns true or false.

No comments:

Post a Comment