1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.

Slides:



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

Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
CS150 Introduction to Computer Science 1
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.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
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 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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
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.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Computer Programming -1-
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
for Repetition Structures
CS150 Introduction to Computer Science 1
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Repetition Statements
Unary Operators ++ and --
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Looping III (do … while statement)
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
do/while Selection Structure
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11

2 10/20/08CS150 Introduction to Computer Science 1 Nested Loops (5.11)  A loop within a loop  Can repeat multiple things within a loop  Example: o Read in 10 grades for each of 20 students o How would we write loops to do this?

3 10/20/08CS150 Introduction to Computer Science 1 What is the Output? for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { // how many times will this run? cout << “*”; } // how many times will this run? cout << endl; }

4 10/20/08CS150 Introduction to Computer Science 1 What is the Output? for (int i = 3; i > 0; i-- ) { for (int j = i; j < i; j++) { cout << “*”; } cout << endl; }

5 10/20/08CS150 Introduction to Computer Science 1 Practice  Write C++ statements that will read in an integer from the user (n) and produce: nnnnnnnnn

6 10/20/08CS150 Introduction to Computer Science 1 do/while Loop (5.5)  So far, we have looked at o while o for o both of these are pretest loops  do/while is another repetition structure  Post-test: test happens at the end of the loop

7 10/20/08CS150 Introduction to Computer Science 1 do/while Loops do { cout << “Enter a year:” << endl; cin >> year; } while (year < 0); // TEST! // The body of the loops happens // before the test

8 10/20/08CS150 Introduction to Computer Science 1 When to use do while?  When loop must execute at least once  Perfect for data validation!  Post-tested loop do { statements; } while ( expression );

9 10/20/08CS150 Introduction to Computer Science 1 What ’ s the output? int m = 10; do { cout << m << endl; m = m - 3; } while (m > 0);

10 10/20/08CS150 Introduction to Computer Science 1 Example  Write C++ statements that will read in integers from the user until the user inputs an integer between 5 and 10.

11 10/20/08CS150 Introduction to Computer Science 1 Rewrite as a do/while loop int num = 0; // what is the output? while (num <= 6) { cout << num << endl; num += 2; }

12 10/20/08CS150 Introduction to Computer Science 1 Rewrite as a do/while Loop // what is the output? for (n = 3; n > 0; n--) { cout << n << “ squared is” << n * n << endl ; }

13 10/20/08CS150 Introduction to Computer Science 1 What is the Output? int counter = 1; do { cout << counter << " "; } while( ++counter <= 10 ); int counter = 1; do { cout << counter << " "; } while( counter++ <= 10 );

14 10/20/08CS150 Introduction to Computer Science 1 What is the Purpose? char ch; do { cout << "Enter a valid code (h,c,i): "; cin >> ch; } while ((ch != 'h') && (ch != 'c') && (ch != 'i'));