01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.

Slides:



Advertisements
Similar presentations
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Advertisements

Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Loops and Files.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
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.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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)
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Computer Programming -1-
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)
Controlling Behavior The if and for Statements.
Branching Constructs Review
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 8 Repetition Statements
Conditinoal Constructs Review
For & do/while Loops.
Conditinoal Constructs Review
Loops/Iteration Used to repeat an action Must have a STOP condition
Repetition Control Structure
Alternate Version of STARTING OUT WITH C++ 4th Edition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Controlling Behavior The if and for Statements.
Presentation transcript:

01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while

01/05/ check the test 2 if the test is true –exec the body –when the body has finished go to step 1 if the test is false –exit the loop int n = ?; // try n as 6 while (n >= 0) { n -= 2; cout << n << endl; } cout << “final n is “ << n << endl; Anatomy of a while loop test ? loop body stmt following loop stmt before loop The test is always a “keep going” condition. To determine the termination condition, negate the test. I.e. the loop will keep going as long as n >= 0 the loop will terminate when n becomes negative (n < 0)

01/05/100 3 while Loops The test is checked at the very beginning and then again each time after the after the entire loop body has been executed The test is NOT checked in the middle of the loop body This is true for all the loops (for, while, and do/while), not just the while loop x = ?; // try x as 45 while (x < 50) { x++; cout << x << endl; x++; cout << x << endl; }

01/05/100 4 Practice what’s the output? int a = 20, b = 50; while (a < b) { if (a % 5 == 0) { cout << “s”; } else if (a % 3 == 0) { cout << “e”; } else { cout << “w”; } a += 4; } cout << “\n”; cout << “a’s final value is “ << a << endl;

01/05/100 5 Practice What’s the output? int d = 90; while (d < 80) { d ++; } cout << “d is “ << d << endl; int x = 90; while (x < 100) { x -= 5; } cout << “final value for x is “ << x << endl;

01/05/100 6 Summing (even) numbers with a while loop Example of an indeterminate loop - the user’s input will determine how many times the loop executes int sum = 0, evensum = 0, number; cout << “ First number please “; cin >> number; while (number > 0) { sum += number; if (number % 2 == 0) { evensum += number; } cout << "number please "; cin >> number; } cout << "sum is " << sum << endl; cout << “sum of even #’s is “ << evensum << endl;

01/05/100 7 Error checking with a while loop if the number is out of range, enter the while loop otherwise, skip the loop reenter loop when the user’s number continues to be out of range const int cMax = 10, cMin = 5; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; while (number cMax) { cout << "error in input \n"; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; } cout << “The user entered “ << number << endl; test ? loop body stmt following loop stmt before loop

Block/Scope { } define a scope void main() { int n = 9; // this n belongs to main { int n = 56; // this n belongs to this block nested in main cout << n << endl; // use the inner most n } cout << n << endl; // don’t know about inner n, use n from main } this is not a good idea, having nested blocks that reuse the same variable name

Scope/Blocks identifiers declared in a nested block cannot be referred to outside the block but identifiers in an outer block are accessible in a nested block void main() { int a = 5; { int x = 9; cout << “inside inner block\n”; cout << x << “ “ << a << endl; } // ILLEGAL, x is not visible in this block cout << x << “ “ << a << endl;} }

Where you really use blocks You won’t use free-standing nested or sequential blocks demo’ed in the last slides, but you do use blocks in constructs that have a pair of { } void main() { int sum = 0; while (sum < 80) { int response; cout << “Please enter a number\n”; cin >> response; sum += response; } cout << response << endl; // ILLEGAL, response is declared inside of the while loop block and cannot be used outside of the while loop cout << sum << endl; // sum can be used both in and out of the while loop }