Alternate Version of STARTING OUT WITH C++ 4th Edition

Slides:



Advertisements
Similar presentations
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.
Advertisements

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?
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.
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: Loops and Files.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Objectives You should be able to describe:
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
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.
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 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.
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.
Fundamental Programming Fundamental Programming for Loops.
Overview Go over parts of quiz? Another iteration structure for loop.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
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)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 5: Loops. Slide 5- 2 Outline Increment and Decrement while loop do-while loop for loop.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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-
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.
Chapter 4 Repetition Structures
Repetitive Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Programming Fundamentals
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5: Looping Starting Out with C++ Early Objects Eighth Edition
Conditinoal Constructs Review
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Additional Control Structures
Repetition Control Structure
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Objectives You should be able to describe: The while Statement
do/while Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Alternate Version of STARTING OUT WITH C++ 4th Edition Chapter 5 Looping Copyright 2004 Scott/Jones Publishing

Topics (continued) 5.8 for Loops 5.9 Deciding Which Loop to Use 5.10 Nested Loops 5.13 Using Loops for Data Validation Chapter 5 slide 2

5.8 for Loops Top test loop that executes zero or more times Useful for counter-controlled loop Format: for( initialization; test; update ) { 1 or more statements; } Required ; No ; goes here Chapter 5 slide 3

for Loop Mechanics for(initialization; test; update) { // {} may be omitted statement(s); // if loop body contains } // only 1 statement Perform initialization Evaluate test expression If true, execute statement(s) If false, terminate loop execution Execute update, then re-evaluate test expression Chapter 5 slide 4

for Loop Flow of Control initialization code update code false test true statement(s) Chapter 5 slide 5

for Loop Example int sum = 0, num; for (num = 1; num <= 10; num++) cout << "Sum of numbers 1 – 10 is " << sum << endl; See pr5-13.cpp Chapter 5 slide 6

for Loop Modifications Can define variables in initialization code Their scope is the for loop Initialization code, test, or update code can contain more than one statement Separate with commas Example: for (int sum = 0, num = 1; num <= 10; num++) sum += num; See pr5-14.cpp Chapter 5 slide 7

5.9 Deciding Which Loop to Use while: pretest loop (loop body may not be executed at all) for: pretest loop (loop body may not be executed at all); has initialization and update code; is useful with counters or if precise number of repetitions is known Chapter 5 slide 8

5.10 Nested Loops A nested loop is a loop inside the body of another loop Example: for (row = 1; row <= 3; row++) { for (col = 1; col <= 3; col++) cout << row * col << endl; } outer loop inner loop See pr5-15.cpp Chapter 5 slide 9

Nested Loops Notes Inner loop goes through all its repetitions for each repetition of outer loop Inner loop repetitions complete sooner than outer loop Total number of repetitions for inner loop is product of number of repetitions of the two loops. In previous example, inner loop repeats 9 times Chapter 5 slide 10

5.13 Using Loops for Data Validation Loops are the most appropriate structure for validating user input data Prompt and read in the data. Use a top test loop to test if data is valid. Enter the loop only if data is not valid. Inside the loop, prompt the user to re-enter the data. The loop will not be exited until valid data has been entered. Chapter 5 slide 11

Data Validation Loop Example cout << “Enter a number (1-100) and” << “ I will guess it. ”; cin >> number; while (number < 1 || number > 100) { cout << “Number must be between 1 and 100.” << “ Re-enter your number. ”; } // Code to use the valid number goes here. See pr5-18.cpp Chapter 5 slide 12

Alternate Version of STARTING OUT WITH C++ 4th Edition Chapter 5 Looping Copyright 2004 Scott/Jones Publishing