0% found this document useful (0 votes)
55 views

While Loops: Christopher Harrison - Content Developer Susan Ibach - Technical Evangelist

Uploaded by

Moiz Taha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

While Loops: Christopher Harrison - Content Developer Susan Ibach - Technical Evangelist

Uploaded by

Moiz Taha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

While Loops

Christopher Harrison | Content Developer


Susan Ibach | Technical Evangelist
Sometimes we need to perform an action more than
once
• Pour a cup of coffee for each guest
• Wash the dishes until they are all clean
• Make a name card for each guest attending a party
Click to edit Master
subtitle style

Loop an unknown number of times


For loops allow us to execute code a fixed number of
times
• So if we know there are twenty guests we can print twenty name
cards

for steps in range(20):     
What if we don’t know how exactly how many times
to repeat an event
• Wash the dishes until they are all clean
• Keep guessing until you get the right answer
• Read all the values in a file or in a database
While Loops allow you to execute until a particular
condition is true
You have to declare the
variable before you use
answer = "0" it in the loop

Execute the code in the loop over


while answer != "4": and over while the variable
answer is not equal to 4

    answer = input("What is 2 + 
2 ")

print ("Yes! 2 + 2 = 4")
Remember only the indented code
is repeated, so this command only
executes after you exit the loop
DEMO
While loop
Can you figure out what this code will do?

import turtle
counter = 0
while counter < 4:
    turtle.forward(100)
    turtle.right(90)
    counter = counter+1

• Yes
While loops can be used instead of for loops

import turtle import turtle
for steps in range(4):      counter = 0
turtle.forward(100)      while counter < 4:
turtle.right(90)     turtle.forward(100)
    turtle.right(90)
    counter = counter+1

• Both loops have the same end


How many lines will this loop draw?

import turtle
counter = 0
while counter <= 4:
    turtle.forward(100)
    turtle.right(90)
    counter = counter+1

• It
How many lines will this loop draw?

import turtle
counter = 1
while counter < 4:
    turtle.forward(100)
    turtle.right(90)
    counter = counter+1

• It
Click to edit Master
subtitle style

Looping issues
How many lines will this loop draw?

import turtle
counter = 0
while counter < 3:
    turtle.forward(100)
    turtle.right(90)
    

• Trick question! It will execute forever! Because the value of


counter is never updated! How can the counter ever become
greater than 3? This is called an endless loop
DEMO
How to get out of an endless loop in Visual Studio
It’s easier to make a mistake with a while loop than a
for loop
• Use for loops whenever possible
Don’t fear the while loop

• You will encounter problems where a while loop is the only way
to solve a problem

Your challenge

• Create an etch a sketch program


• Have the user enter a pen color, a line length, and an angle
• Use turtle to draw a line based on their specifications
• Let them specify new lines over and over until they enter a line
length of 0.

Congratulations

• You can manage problems


which require repeating the
same task and end when a
specific condition occurs
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the
U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft
must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

You might also like