More Repetition While and For Loops SentinelControlled Loops

  • Slides: 13
Download presentation
More Repetition While and For Loops Sentinel-Controlled Loops Intro to Computer Science CS 1510

More Repetition While and For Loops Sentinel-Controlled Loops Intro to Computer Science CS 1510 Dr. Sarah Diesburg

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

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

Some Things From PA 02 BMI These were continuous regions. Set up your code

Some Things From PA 02 BMI These were continuous regions. Set up your code to handle all areas…

Some Things From PA 02 BMI While this is valid mathematically, it is bad

Some Things From PA 02 BMI While this is valid mathematically, it is bad form in programming languages. And it causes real problems when not set up properly.

Some Things From PA 02 BMI Recognize that these are four related categories. This

Some Things From PA 02 BMI Recognize that these are four related categories. This makes it much easier to use if/else.

Some Things From PA 02 BMI Recognize that these are four related categories. This

Some Things From PA 02 BMI Recognize that these are four related categories. This makes it much easier to use if/else.

Some Things From PA 02 BMI Recognize that these are four related categories. This

Some Things From PA 02 BMI Recognize that these are four related categories. This makes it much easier to use if/else.

Loops Count-controlled loop, which means we will know in advance how many times the

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 8

For Loops for var. Name in iterable. Data. Structure: (next thing in Data. Structure

For Loops for var. Name in iterable. Data. Structure: (next thing in Data. Structure put in var. Name) suite of code Is a for loop count-controlled or sentinelcontrolled? 9

While Loops while boolean expression: statement. Suite If while loop is count-controlled, will it

While Loops while boolean expression: statement. Suite If while loop is count-controlled, will it contain some kind of counter? 10

Moving to Sentinel Controlled For loops are always count-controlled Every for loop could be

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).

Let’s go back to Thursday’s Lab The “challenge” of using a while loop is

Let’s go back to Thursday’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)

Let’s look at some code

Let’s look at some code