Let’s all Repeat Together

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
CS161 Introduction to Computer Science
for Repetition Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Control Statements Kingdom of Saudi Arabia
Repetition Structures (Loops)
The while Looping Structure
Unary Operators ++ and --
The while Looping Structure
Control Statements Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
2.6 The if/else Selection Structure
Life is Full of Alternatives
Let’s all Repeat Together
Arithmetic Operations
do/while Selection Structure
Reading from and Writing to Files
Life is Full of Alternatives
Control Statements Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops.
The while Looping Structure
Chapter 4 Repetition Structures
Life is Full of Alternatives Part 3
The while Looping Structure
Reading from and Writing to Files
Presentation transcript:

Let’s all Repeat Together 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Looked at complex examples that use the if selection structure Covered the if/else selection structure Learnt about the ?: conditional operator Learn how to nest if/else selection structures Today we will Start looking at examples of repetition structures 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem 9.6: Write a program that displays a letter grade corresponding to an exam score 90 - 100 A 80 - 89 B 70 - 79 C 60 - 69 D 0-59 F 9/28/05 CS150 Introduction to Computer Science 1

Repetition Structures All the C++ programs that we have seen so far are executed only once before the program terminates However, it is often the case that programmers would like to specify that an action continue repeating while a condition is true This is achieved by using repetition structures, also called loops 9/28/05 CS150 Introduction to Computer Science 1

An Example of Repetition An example of where we might need to use repetition is if we are calculating the average grade of a class of students We would need to continue reading in student grades until we have covered all students In pseudocode this might be: While there are more students in the class Ask for student grade 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 C++ Example Write the segment of C++ code that will sum five numbers entered by the user int sum, counter, num; sum = 0; counter = 0; while( counter < 5 ) { cout << "Enter a number: "; cin >> num; sum = sum + num; counter = counter + 1; } cout << "The sum of your numbers is: " << sum; 9/28/05 CS150 Introduction to Computer Science 1

while Repetition Structure int sum, counter, num; sum = 0; counter = 1; while( counter <= 5 ) { cout << "Enter a number: "; cin >> num; sum = sum + num; counter = counter + 1; } cout << "The sum of your numbers is: " << sum; Initialize LCV Loop Control Variable Change the value of counter 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 while loops The syntax for while loops is while (condition is true) statement; { statement1; statement2; … } 9/28/05 CS150 Introduction to Computer Science 1

Key Ingredients of while loops Initialize MUST initialize loop control variable Test The value of the loop control variable is tested during each iteration of loop Update Loop control variable is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely. 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problems Write a while loop that outputs each integer from 1 to 5 What’s the output for x = 2? 3? 5? cout << “Enter an integer”; cin >> x; product = x; count = 0; while (count < 4) { cout << product << endl; product *= x; count += 1; } 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problems Write a program that reads in the salary of 5 employees and calculates the gross pay We know, before the program runs, how many times the loop will iterate Counter-controlled repetition Write a program that reads an undetermined number of student grades and calculates the average student grade We don’t know, before the program runs, how many times the loop will iterate Sentinel-controlled repetition 9/28/05 CS150 Introduction to Computer Science 1

Counter-Controlled Repetition We know, before we run the program, the number of repetitions that the loop will make Also called definite repetition Write a program that reads in the salary of 5 employees and calculates the gross pay 9/28/05 CS150 Introduction to Computer Science 1

Sentinel-Controlled Repetition We have no idea how many times the loop will need to iterate Write a program that reads an undetermined number of student grades and calculates the average student grade How will we know when we’ve read all employee’s salaries? i.e. How will we know when to stop looping? 9/28/05 CS150 Introduction to Computer Science 1

Sentinel-Controlled Repetition Use a sentinel value User types employee salaries until all legitimate salaries have been entered User then types in sentinel value to indicate that there are no more legitimate salaries Also called indefinite repetition Sentinel value must be chosen so that it cannot b confused with legitimate inputs -1 is a good value to use in most cases 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program that reads an undetermined number of student grades and calculates the average student grade 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution #include “stdafx.h” #include <iostream> using namespace std; int main() { int gradeCounter; int grade; double average; double total; total = 0; gradeCounter = 0; 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution cout << "Enter grade, -1 to end: "; cin >> grade; while ( grade != -1 ) { total = total + grade; gradeCounter = gradeCounter + 1; } if ( gradeCounter != 0 ) average = total / gradeCounter; cout << "Class average is " << average << endl; else cout << "No grades were entered" << endl; 9/28/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered while repetition structure Readings P. 81 - 83: while repetition structure 9/28/05 CS150 Introduction to Computer Science 1