Search This Blog

Saturday, August 27, 2016

While Loops

Today we are going to focus on the while loop and some different ways we can use them.
    You can make a block of code execute over and over again as long as the while statement is true.
So let's open up a new file editor window by opening idle and going to file, new file. Or pressing ctrl + n.

In the window let's enter the following code:

spam = 0
While spam < 5 :
print(‘Hello World’)
Spam = spam +1

When you hit F5 to run it. The string Hello World will be printed 5 times. This is because spam starts off at zero and adds to it on every iteration of the loop. As long as the spam variable is less than one it will keep looping until the condition is met. When the count reaches 5 it jumps out of the loop and carries on with the script.

When the execution runs through a loop, we call that an iteration. We can say that this iterated through the loop 5 times. On each iteration the spam variable was increased by 1.

Last lesson we talked about if statements, Let's compare the difference between while loops and if statements.


Here we have a while and if statements. The difference is how they behave. On the if statement, after the execution is completed it continues through the script. In the while loop it keeps checking if the condition is met to carry on.

Lets try writing a new program. Open up a new file editor window and enter the following code.


name = ‘’
while name != ‘your name’:
    print(‘Please print your name.’)
    name = input()
print(‘Thank you’)




This is an example of input validation. When the input function is called the user can type in anything. Unless it is the string that the program wants it will stay within the loop until the condition is met.

Infinite Loops and Ctrl+C
There is a specific kind of bug you can have with loops called infinite loops:

Let's type this into the interactive shell.

>>>While True:
    print(‘Hello!!’)

This is a bug. With this script the condition of True will never change.
You can get out of it with a ctrl+c combination.

There are two new kinds of statements that can go inside a while loop. break() and continue()
The break statement causes the execution to immediately jump out of the loop doesn’t even check the condition again it just immediately jumps out of the loop.

Lets take this script we wrote earlier and change it a little:

name = ‘’
while True:
    print(‘Please print your name.’)
    name = input()
    If name == ‘your name’:
        break
print(‘Thank you’)

Just like the other script this one asks the user to literally type in ‘your name’ and it will keep looping until you do. In this program however we don’t break out of the loop if the condition is false, it will never be false. Instead what we have is an if statement that checks if the user typed in the corrected validated statement then it will immediately break out of the loop and continue through the program. And prints thank you.

Here is the updated flow chart that shows the flow of the break statement,


The other statement is the continue statement. Like break statements the continue statement is also used inside loops.  With a continued statement, instead of jumping out of the loop it goes back and re evaluates the condition of the loop.


>>>spam =0
>>> while spam < 5:
   spam = spam +1
   if spam == 3:
      continue
   print('spam is ', spam)

When the spam variable gets to 3 it hits the continue block and doesn’t make it to the print statement, it jumps up to the beginning of the loop and carries on.


No comments:

Post a Comment