Mastery objective
Sponsored Links
This presentation is the property of its rightful owner.
1 / 15

Mastery Objective: PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

Mastery Objective:. Students will understand how to use while loops in computer programming. Agenda.

Download Presentation

Mastery Objective:

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Mastery objective

Mastery Objective:

Students will understand how to use while loops in computer programming



  • If-else and if-elif-else assignment due tomorrow (Wednesday) by the end of class with no scheduled in class time. If you haven’t finished the assignment, plan to stay after or come in early to finish the assignment.

  • Notes: While loops

  • Programming assignments

While loops

While loops

Basic principle:

  • While something is true, repeat something

    Basic structure:

    while response != “Because.”:

    response = raw_input(“Why? “)

Similarities and differences between if and while structure

Similarities and differences between if and while structure

  • In both structures, if the condition is true, the block is executed.

  • What is different: In the while structure, the block is executed until it is false.

Sentry variable

Sentry Variable

  • Often, sentry variables control the while loop

  • response variable in example code

    while response != “Because.”:

    response = raw_input(“Why? “)

  • Must be intialized prior to use, usually right before while loop. Usually initialized to empty variable

    response = “ “



response = “”

while response !=“No”:

response = “raw_input(“Try again!”):

print “All done!”

Infinite loop

Infinite Loop

  • Loop that never ends.

  • Example

    counter = 0

    while counter <=10

    print counter

    Forgot to increase counter variable

    Counter +=1

How to stop an infinity loop

How to Stop an Infinity Loop

  • Control key +C

Setting values as true or false

Setting values as true or false

When using numbers: 0 is false other numbers are true

if money:

print “Thank you!”


print “I am sorry there are no seats available.”

Intentional infinity loops

Intentional Infinity loops

  • There is a place for infinite loops

    Count = 0

    While Ture:

    count +=1

    if count >10:

    break (breaks loop)

    if count ==5;

    continue (jump back to top of loop)

    print count

Break and continue

Break and Continue

  • Used in any type of loop

  • Should be used sparingly

Using compound conditions

Using compound conditions

  • Uses logical operators

Not logical operator example

not Logical Operator Example

username = “ “

while not username:

username = raw_input(“Username: “)

Loop continues to ask for a username until the user enters something

And logical operator

And Logical Operator

elif username == “S. Meier” and password ==“civilization”:

Or logical operator example

Or Logical operator example

elif username==“guest” or password ==“guest”:

print “Welcome, guest.”

  • Login