Chapter 4 - Loops. Loops are important for calculations that require repeated steps and for processing input consisting of many data items. In this chapter you will learn about loop statements in C++, as well as techniques for writing programs that process input and simulate activities in the real world. | C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved To implement while, for and do loops To avoid infinite loops and off-by-one errors To understand nested loops To implement programs that read and process data sets To use a computer for simulations Chapter Goals C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved A loop is a statement that is used to: execute one or more statements repeatedly until a goal is reached. Sometimes these one-or-more statements will not be executed at all —if that’s the way to reach the goal What Is the Purpose of a Loop? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ has these three looping statements: while for do The Three Loops in C++ C++ for Everyone by Cay Horstmann Copyright © 2012 | C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved To implement while, for and do loops To avoid infinite loops and off-by-one errors To understand nested loops To implement programs that read and process data sets To use a computer for simulations Chapter Goals C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved A loop is a statement that is used to: execute one or more statements repeatedly until a goal is reached. Sometimes these one-or-more statements will not be executed at all —if that’s the way to reach the goal What Is the Purpose of a Loop? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved C++ has these three looping statements: while for do The Three Loops in C++ C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The while Loop In a particle accelerator, subatomic particles traverse a loop-shaped tunnel multiple times, gaining speed. Similarly, in computer science, statements in a loop are executed while a condition is true. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The while Loop The while statement executes statements until a condition is true Now? Now? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The while Loop The while statement executes statements until a condition is true Nope, do something to get closer to the end! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The while Loop The while statement executes statements until a condition is true C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved The while Loop The while statement executes statements until a .