Controlling execution - iteration

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
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)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
 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 Chapter 9 Additional Control Structures Dale/Weems.
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 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Control Structures RepetitionorIterationorLooping Part I.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
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 4 Repetition Structures
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
C++ Programming: CS150 For.
A Lecture for the c++ Course
Beginning C++ Programming
Engineering Problem Solving with C++, Etter/Ingber
Arithmetic & other operations
Programming Fundamentals
CSCI 161: Introduction to Programming
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.
Object-oriented programming principles
Lecture 4B More Repetition Richard Gesick
Chapter 4 Repetition Structures
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.
Chapter 4 Repetition Structures
One-Dimensional Array Introduction Lesson xx
Introduction to Programming
Additional Control Structures
Computing Fundamentals
Control Structures Part 2
Control Structures Part 1
Let’s all Repeat Together
2.6 The if/else Selection Structure
Fundamental Programming
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Presentation transcript:

Controlling execution - iteration A Lecture for the c++ Course Each slide has its own narration in an audio file. For the explanation of any slide, click on the audio icon to start the narration. The Professor‘s C++Course by Linda W. Friedman is licensed under a  Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Iteration constructs Iteration ≡ Repetition ≡ Looping In order to control iteration, we use one of three structured control statements. for while do/while The for structure is a counting loop. The while structure is a “test before” loop. The do/while structure is a “test after” loop. controlling execution - iteration

A counting program We would like to write a program that will print the integers between 1 and 20, say, like the output shown here: This is one way: // A counting program using a for Loop #include <iostream> using namespace std; int main(){ int count ; for (count=1; count<=20; count++) cout << count << endl; return 0; } controlling execution - iteration

Another counting program Two other programs that produce the same output. First this one: // A counting program using a while/do Loop #include <iostream> using namespace std; int main(){ int count; count = 1; while (count <=20){ cout << count << endl; count = count + 1; } return 0; controlling execution - iteration

Yet Another counting program And this one // A counting program using a do/while Loop #include <iostream> using namespace std; int main(){ int count; count = 1; do { cout << count << endl; count = count + 1; } while (count <=20);   return 0; } controlling execution - iteration

The variables sum and count are accumulators. A summing program We wish to write a program that will calculate the sum of the integers between 1 and 20. // A Summing Program using a while/do Loop #include <iostream> using namespace std; int main(){ int sum, count ; sum = 0; count = 1; while (count <=20){ sum = sum + count; count = count + 1; } cout << "The sum of the integers from 1 through 20 is "; cout << sum << endl; return 0; The variables sum and count are accumulators. controlling execution - iteration

A summing program This program produces the same output. Which is better? // A Summing Program using a for loop #include <iostream> using namespace std; int main(){ int sum, count ; sum = 0; for (count=1; count<=20; count++) sum = sum + count; cout << "The sum of the integers from 1 through 20 is "; cout << sum << endl; return 0; } controlling execution - iteration

Just an average program We have already seen a program that calculates the average of 3 particular numbers. That’s not very useful because it is not general enough. We can do better now that we know how to loop. Here are some runs to show how the output from this program should look. controlling execution - iteration

Just an average program This program uses a for loop: #include <iostream> int main(){ int n, count; float x, sum, avg; sum = 0; cout << "How many numbers? "; cin >> n; for (count=1; count<=n; count++){ cout << "? "; cin >> x; sum = sum + x; } avg = sum / n; cout << "The average is " << avg << endl; return 0 Do we really need to ask the user to count? Counting is something computers do really well! controlling execution - iteration

Run this program yourself to see how it works. Using a sentinel value #include <iostream> int main(){ int count; float x, sum, avg; const float EOD = -999; //end of data indicator count = 0; sum = 0; cout << “First number?”; cin >> x; while (x != EOD) { sum += x; count++; cout << "? "; cin >> x; } avg = sum / count; cout << "The average is " << avg << endl; return 0; Run this program yourself to see how it works. controlling execution - iteration

Choosing a loop structure When to use the while structure? When to use for? How about do/while? controlling execution - iteration

Branching – emergency use only Discuss conditional vs. unconditional branching statements. The goto statement. Branching instructions should be used sparingly, only when absolutely necessary. We will never use the unconditional goto statement, so we won’t even cover it here. Conditional branching statements: break continue controlling execution - iteration

break The break statement is used to break out of a loop. Control is transferred to the first statement after the end of the loop. Example. What is output? // modified from the Deitel book // Using the break statement in a for structure #include <iostream> using namespace std; int main(){ int x; for (x=1; x<=10; x++) { if (x==5) break; //break loop only if x is 5 cout << x << " "; } //end for cout << "\nBroke out of loop at x = " << x << endl; cout<< endl << endl << endl; return 0; } controlling execution - iteration

Example using break controlling execution - iteration

Continue The continue statement skips the remainder of the body of the loop, and continues the loop. Transfers control to “loop again.” // This program is modified from the Deitel book, #include <iostream> using namespace std; int main(){ int x; for (x=1; x<=10; x++) { if (x==5) continue; // skip remaining code in loop only if x is 5 cout << x << " "; } cout << "\nUsed continue to skip printing the value 5 " << endl; return 0; controlling execution - iteration

Counting random digits In the following example, the function rand() defined in the header file <stdlib> generates pseudorandom unsigned integers in the range of 0 to RAND_MAX. RAND_MAX is a constant which is also defined in <stdlib>. // This program gets 1000 random numbers and tests them #include <iostream> #include <stdlib> int main(){ int num0, num1, num2, num3, num4, num5, num6, num7, num8, num9; num0=num1=num2=num3=num4=num5=num6=num7=num8=num9=0; for (int i=0; i<1000; i++) { int r = rand()%10; //remainder gives a one digit RN //between 0 and 9 … program continues on next slide controlling execution - iteration

Counting random digits for (int i=0; i<1000; i++) { int r = rand()%10; //remainder gives a one digit RN //between 0 and 9 switch (r) { case 0: num0++; break; case 1: num1++; break; case 2: num2++; break; case 3: num3++; break; case 4: num4++; break; case 5: num5++; break; case 6: num6++; break; case 7: num7++; break; case 8: num8++; break; case 9: num9++; break; default: cout << "Error: unexpected random number" << r; } //end switch } //end for … program continues on next slide Note: This program will be more efficient when we learn arrays. controlling execution - iteration

Counting random digits … end of program cout << "number of 0 : " << num0 << endl; cout << "number of 1 : " << num1 << endl; cout << "number of 2 : " << num2 << endl; cout << "number of 3 : " << num3 << endl; cout << "number of 4 : " << num4 << endl; cout << "number of 5 : " << num5 << endl; cout << "number of 6 : " << num6 << endl; cout << "number of 7 : " << num7 << endl; cout << "number of 8 : " << num8 << endl; cout << "number of 9 : " << num9 << endl; return 0; } //end main  controlling execution - iteration

Review What did we learn in this lecture? Plenty. Some terms to jog your memories: Iteration Branching instruction Looping Unconditional branch Repetition Conditional branch Accumulator variable Goto Counter variable Break statement For Continue statement Do/while Pseudorandom number While/do Sentinel value controlling execution - iteration