C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

Repeating Actions While and For 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.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
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.
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 5: Control Structures II (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)
Chapter 5: Loops and Files.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: 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.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
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 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.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 4 Repetition Structures
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
Control Structures II (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.
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.
A First Book of ANSI C Fourth Edition
Chapter 2.1 Repetition.
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4 Repetition Structures
Presentation transcript:

C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming techniques

C++ for Engineers and Scientists, Third Edition2 Objectives (continued) Nested loops do while loops Common programming errors

C++ for Engineers and Scientists, Third Edition3 Basic Loop Structures Repetition structure has four required elements: –Repetition statement –Condition to be evaluated –Initial value for the condition –Loop termination Repetition statements include: –while –for –do while

C++ for Engineers and Scientists, Third Edition4 Basic Loop Structures (continued) The condition can be tested –At the beginning: Pretest or entrance-controlled loop –At the end: Posttest or exit-controlled loop Something in the loop body must cause the condition to change, to avoid an infinite loop, which never terminates

C++ for Engineers and Scientists, Third Edition5 Pretest and Posttest Loops Pretest loop: Condition is tested first; if false, statements in the loop body are never executed while and for loops are pretest loops Figure 5.1 A pretest loop

C++ for Engineers and Scientists, Third Edition6 Pretest and Posttest Loops (continued) Posttest loop: Condition is tested after the loop body statements are executed; loop body always executes at least once do while is a posttest loop Figure 5.2 A posttest loop

C++ for Engineers and Scientists, Third Edition7 Fixed-Count Versus Variable- Condition Loops Fixed-count loop: Loop is processed for a fixed number of repetitions Variable-condition loop: Number of repetitions depends on the value of a variable

C++ for Engineers and Scientists, Third Edition8 while Loops while statement is used to create a while loop –Syntax: while (expression) statement; Statements following the expressions are executed as long as the expression condition remains true (evaluates to a non-zero value)

C++ for Engineers and Scientists, Third Edition9 while Loops (continued)

Lab Ex. P. 247 Ex. 2 C++ for Engineers and Scientists, Third Edition10

C++ for Engineers and Scientists, Third Edition11 Interactive while Loops Combining interactive data entry with the while statement provides for repetitive entry and accumulation of totals

C++ for Engineers and Scientists, Third Edition12 Interactive while Loops (continued) Figure 5.6 Accumulation flow of control

C++ for Engineers and Scientists, Third Edition13 Sentinels Sentinel: A data value used to signal either the start or end of a data series Use a sentinel when you don’t know how many values need to be entered

C++ for Engineers and Scientists, Third Edition14 break and continue Statements break statement –Forces an immediate break, or exit, from switch, while, for, and do-while statements –Violates pure structured programming, but is useful for breaking out of loops when an unusual condition is detected

break and continue Statements (continued) Example of a break statement: C++ for Engineers and Scientists, Third Edition15

break and continue Statements (continued) A continue statement where invalid grades are ignored, and only valid grades are added to the total: C++ for Engineers and Scientists, Third Edition16 // onto the next iteration

C++ for Engineers and Scientists, Third Edition17 break and continue Statements (continued) continue statement –Applies to while, do-while, and for statements; causes the next iteration of the loop to begin immediately –Useful for skipping over data that should not be processed in this iteration, while staying within the loop

C++ for Engineers and Scientists, Third Edition18 for Loops (continued)

C++ for Engineers and Scientists, Third Edition19 for Loops for statement: A loop with a fixed count condition that handles alteration of the condition –Syntax: for (initializing list; expression; altering list) statement; Initializing list: Sets the starting value of a counter Expression: Contains the maximum or minimum value the counter can have; determines when the loop is finished

C++ for Engineers and Scientists, Third Edition20 A Closer Look: Loop Programming Techniques (continued)

C++ for Engineers and Scientists, Third Edition21 A Closer Look: Loop Programming Techniques (continued)

Nested Loops

C++ for Engineers and Scientists, Third Edition23 Nested Loops Nested loop: A loop contained within another loop –All statements of the inner loop must be completely contained within the outer loop; no overlap allowed –Different variables must be used to control each loop –For each single iteration of the outer loop, the inner loop runs through all of its iterations

Pp Ex.4

Send lab work To: Subject: , Ex.5.6.4, CS125, 2013

Do while #include using namespace std; int main() { double grade; do { cout << "\nEnter a grade: "; cin >> grade; } while (grade 100); // Grades should be between 0 and 100. // If not valid, go back to enter again. cout << "\nThe grade entered was: " << grade << endl; }

C++ for Engineers and Scientists, Third Edition27 do while Loops do while loop is a posttest loop –Loop continues while the condition is true –Condition is tested at the end of the loop –Syntax: do statement; while (expression); All statements are executed at least once in a posttest loop

C++ for Engineers and Scientists, Third Edition28 do while Loops (continued)

C++ for Engineers and Scientists, Third Edition29 Summary Loop: A section of repeating code, whose repetitions are controlled by testing a condition Three types of loops: – while – for – do while Pretest loop: Condition is tested at beginning of loop; loop body may not ever execute; ex., while, for loops

C++ for Engineers and Scientists, Third Edition30 Summary (continued) Posttest loop: Condition is tested at end of loop; loop body executes at least once; ex., do while Fixed-count loop: Number of repetitions is set in the loop condition Variable-condition loop: Number of repetitions is controlled by the value of a variable