1 / 13

More Repetition While and For Loops Sentinel-Controlled Loops

More Repetition While and For Loops Sentinel-Controlled Loops. Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg. Today’s Agenda. Exploring looping alternatives For and While loops Sentinel-Controlled vs Count-Controlled loops. Questions?. PA3?

lionelv
Télécharger la présentation

More Repetition While and For Loops Sentinel-Controlled Loops

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Repetition While and For Loops Sentinel-Controlled Loops Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

  2. Today’s Agenda • Exploring looping alternatives • For and While loops • Sentinel-Controlled vs Count-Controlled loops

  3. Questions? • PA3? • (Remember, Elise is available to help with homeworks 3-4pm M/W in WRT 339) • The lab?

  4. Loops • Count-controlled loop, which means we will know in advance how many times the loop will run • Sentinel-controlled loop, which means we do not know in advance how many times the loop will run • Controlled by sentinels • Event-controlled

  5. For Loops for varName in iterableDataStructure: (next thing in DataStructure put in varName) suite of code • Is a for loop count-controlled or sentinel-controlled?

  6. While Loops while boolean expression: statementSuite • If while loop is count-controlled, will it contain some kind of counter?

  7. Let’s go through a count-controlled example… • Annoying kid in the car (cartrip.py)

  8. For loop for counter in range(age): print("Are we there yet?")

  9. While loop counting up counter=0 while counter<age: print("Are we there yet?") #counter=counter+1 counter += 1

  10. While loop counting down while age>0: print("Are we there yet?") age -= 1 #But now age is destroyed!

  11. Moving to Sentinel Controlled • For loops are always count-controlled • Every for loop could be written as a while loop (although usually a little more complicated to set up) • While loops can behave like count controlled loops (kid in the car from this week) but also as sentinel-controlled loops (average quiz score from this week).

  12. Let’s go back to Tuesday’s Lab • The “challenge” of using a while loop is that it is a pre-test solution. • That is, you have to have some data to work with • Several solutions to the “average” problem • Adjusting for the extra loop (example 1) • Using a “loop and a half” (example 2) • Infinite loops with a break statement (example 3)

  13. Let’s look at some code

More Related