Control Structures RepetitionorIterationorLooping Part I.

Slides:



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

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
 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.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures Repetition or Iteration or Looping Part II.
Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Control Structures RepetitionorIterationorLooping Part I.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
Control Structures Repetition or Iteration or Looping Part II.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
Chapter 2.2 Control Structures (Iteration)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
While Loops.
Outline Altering flow of control Boolean expressions
Additional Control Structures
Loops A loop is a repetition control structure.
A First Book of ANSI C Fourth Edition
3 Control Statements:.
Chapter 2.2 Control Structures (Iteration)
Control Structures Repetition or Iteration Looping Part I.
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Presentation transcript:

Control Structures RepetitionorIterationorLooping Part I

Sequence Print out a sales report Open the salesperson file Print heading on form Skip 3 lines Read the first record Print salesperson’s name Also called “Straight-Line” Programs

Decision (selection, branching) sales > quota yes bonus=sales*.01 no bonus = 0 Sequential prior to if { sequential inside an if } true Sequential after an if false blockblock

Switch (Multiple Decision) Option=1 Option=2 Option=3 Option=4 Can always be written as nested if Assumes no multiple cases, break for each, no default

Repetition (iteration, looping) More? Comm = Sales*.02 Calculate Pay Print Check No Yes Sequential inside of the loop true false After the loop, continue sequentially with next statement

The while Statement Syntax while (expression) statement Example: count = 1; while (count <= 10) { cout << “Yankees are #1\n”; count = count + 1; } next statement *

The while Statement Syntax while (expression) statement ã a loop control variable is evaluated in the expression ã the loop statements contain the lines executed each time the loop repeats

The while Statement loop Exit the while 0 or False Test the expression statements to execute 1 or True

Something to Note Note Note... count = 1; while (count <= 10) { cout << “Yankees are #1\n”; cout << “and will win the series\n”; } next statement * How do we get out?

The while Statement ã loop control variable is initialized before while statement ã evaluation or test is performed within the expression ã the body may contain any number of statements, including branches and other loops ã the control variable is changed during loop execution in order to exit loop ã the statement immediately after the while is executed upon exiting * * * * *

int num; cout << "NUMBER SQUARE CUBE\n" << " \n"; A Simple Example num = 1; while (num < 11) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num <<‘\n’; num++; // increment num } * * *

Simple Example Output NUMBER SQUARE CUBE …

double celsius, fahren; cout << "CELSIUS FAHRENHEIT\n" << " \n"; Another Example celsius = 5; // starting Celsius value while (celsius <= 50) { fahren = (9.0/5.0) * celsius ; cout << setw(4) << celsius << setiosflags(ios::showpoint) << setw(13) << setprecision(2) << fahren << '\n'; celsius = celsius + 5; } Note: The text shows CONSTANTS in this program on page 179 * * *

Problem Solving: Finding the Largest Value â The program asks for the number of items in the list. â Checks to see if that number is positive. â Gets user input. â Assigns the largest to variable max. * *

int count = 0, n = 0; double max = 0, x = 0; int count = 0, n = 0; double max = 0, x = 0; cout << "The maximum value will be computed.\n"; cout > n; while (n > n; } cout > x; max = x;// first value to max * *

while (count++ > x; if (max > x; if (max < x) max = x; } cout << “Maximum value: “ << max << “\n”; } Output The maximum value will be computed. How many numbers do you wish to enter? 4 Enter a real number: 1.01 Enter a real number: -3 Enter a real number: 2.2 Enter a real number: Maximum value: 7.07 * * *

Counter++ n = 5; count = 0; cout > x; max = x; cout > x; max = x;// count = 0 1while (count++ < (n-1)) { 2 { cout << “LOOP number: “ 3 cout << “LOOP number: “ cin >> x; if (max < x) 4 if (max < x) max = x; 5 max = x; } 6 } cout <<“Max is “<<max; loop countn executed *

Common Use: Running Totals (Accumulator) while (count > num; total = total + num; cout > num; total = total + num; cout << “The total is “ << total; count++; } count =1; // variables must be initialized total = 0; // before loop *

Common Use: Average While (count <=4) { cout <<“Enter a number ”; cout <<“Enter a number ”; cin >> number; cin >> number; total += number; total += number; count++; count++;} average = total / (count-1); cout << “The average is “ << average << endl; count =1; total = 0; float average; Why count – 1?

Sentinel Loops – exit controlled by user Different from fixed-count loops Actually “variable-condition” loops User can be asked: “Do you want to continue?” Or, entry of unusual data value will end loop Book example: Program 5-8 on page 189 A grades program, where entry of a grade higher than 100 will exit the loop

Sentinel example grade = 0; total = 0; cout << “To stop, type number higher than 100”; while (grade < 100) {total = total + grade; cout << “Enter a grade: “; cin >> grade; } cout << “\nThe total of the grades is “ << total << endl;

Common Errors Improper braces in nested structures Using = in place of == != versus == This changes the logic, be especially careful when used with && or || infinite loops: != versus && versus || *