Chapter05.ppt
Document Details
Uploaded by IndebtedOwl
2017
Tags
Full Transcript
Chapter 5: Looping Starting Out with C++ Early Objects Ninth Edition, Global Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Copyright © 2017 Pearson Educati...
Chapter 5: Looping Starting Out with C++ Early Objects Ninth Edition, Global Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Copyright © 2017 Pearson Education, Ltd. Topics 5.1 Introduction to Loops: The while Loop 5.2 Using the while loop for Input Validation 5.3 The Increment and Decrement Operators 5.4 Counters 5.5 Keeping a Running Total Copyright © 2017 Pearson Education, Ltd. 5-2 Topics (continued) 5.7 The do-while loop 5.8 The for loop 5.9 Deciding Which Loop to Use 5.10 Nested Loops 5.11 Breaking Out of a Loop Copyright © 2017 Pearson Education, Ltd. 5-3 5.1 Introduction to Loops: The while Loop Loop: a part of a program that may execute > 1 time (i.e., it repeats) while loop format: while (condition) { statement(s); No ; here } The {} can be omitted if there is only one statement in the body of the loop Copyright © 2017 Pearson Education, Ltd. 5-4 How the while Loop Works while (condition) { statement(s); } condition is evaluated – if it is true, the statement(s) are executed, and then condition is evaluated again – if it is false, the loop is exited An iteration is an execution of the loop body Copyright © 2017 Pearson Education, Ltd. 5-5 while Loop Flow of Control Copyright © 2017 Pearson Education, Ltd. 5-6 while Loop Example int val = 5; while (val >= 0) { cout