More Repetition While and For Loops Sentinel-Controlled Loops

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
An Introduction to Programming with C++ Fifth Edition
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Lab 07: Caesar Cypher Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Today’s Agenda 1.Collect Pre-Lab 3 2.Alice Programming Assignment 3.Built-in Functions 4.Expressions 5.Control Structure 6.Assign pair programming teams.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS122 Engineering Computation Lab Lab 2 Bruce Char Department of Computer Science Drexel University Winter 2012.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
Introduction To Repetition The for loop
Repetition (While-Loop) version]
for Repetition Structures
Quick Test What do you mean by pre-test and post-test loops in C?
Topics discussed in this section:
Web Programming– UFCFB Lecture 16
Control Structures - Repetition
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Looping and Repetition
More Nested Loops and Lab 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
CS 1430: Programming in C++ No time to cover HiC.
Types, Truth, and Expressions (Part 2)
Introduction to Object-Oriented Programming with Java--Wu
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Looping and Random Numbers
Chapter 4 Loops While loop The for loop do… while break and continue
Thinking about Strings
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS150 Introduction to Computer Science 1
More on Functions (Part 2)
Intro to Nested Looping
Let’s all Repeat Together
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
More Repetition While and For loop variations
Introduction to Computer Science
Programming Right from the Start with Visual Basic .NET 1/e
Repetition Statements (Loops) - 2
Intro to Nested Looping
Types, Truth, and Expressions
CS2911 Week 3, Lab Today Thursday Friday Review Muddiest Point Lab 3
Millennium High School Agenda Calendar
Millennium High School Agenda Calendar
The while Looping Structure
Introduction to Strings
Looping and Repetition
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Presentation transcript:

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

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

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

Some Things From PA02 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 PA02 BMI Recognize that these are four related categories. This makes it much easier to use if/elif/else.

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

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

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

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?

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

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