Chapter 4 Repetition Structures

ITP 134 C++ Study Guide

Chapter 4 Repetition Structures

Instructions: Use this Study Guide to help you understand the important points of this chapter. See the book for lots of great programming examples. Important concepts, keywords, statements and functions are shown in emphasized font.
Code syntax and examples are shown in code font.

Games & Graphics in C++
2nd edition by Tony Gaddis

ITP 134 – Mrs. EatonChapter 4 Study Guide – 2nd EditionPage 1

Chapter 4 Repetition Structures

Control Structures

CONCEPT: Control structures affect the order in which statements execute. There are 3 main types of control structures, sequence, selection, and repetition. (pg 87)

  1. Sequence – set of statements that execute in the order that they appear
  2. Selection (also called decision) – structure used to create a program with more than one path of execution (pg 89)
  3. Repetition (also called loop or iteration ) – structure used to repeat the same set of statements a number of times (pg 89)

Combine Control Structures (Not in book, from CSC 200 class)

  • Each control structure has a single entry and a single exit point.
  • Two ways to combine control structures, stack or nest.

4.1 Introduction to Repetition Structures

CONCEPT: A repetition structure causes a statement or set of statements to execute repeatedly. (pg 131)

A repetition structure is also known as a loop.

In this chapter we will look at 2 categories of loops:

  1. Condition-controlled loop
    While and do-while statements are condition-controlled loops because they use a true/false condition to control the number of times it repeats.
  2. Count controlled loops
    A for loop is a count-controlled loop because it repeats a specific number of times.

4.2Condition-Controlled Loops: The while Loop and the do-while loop

CONCEPT: Both the while and do-while loops cause a statement or a set of statements to repeat as long as the condition is true.

The while Loop

The while loops gets its name from the way it works. While a Boolean expression is true, do some task. (page 133) See the previous page for a diagram that shows the difference between a pretest while loop and a posttest do while loop.

The while Loop is a Pretest Loop

The while loop is known as a pretest loop, which means it tests the expression before performing the first iteration. (page 136)

Infinite Loops

Loops should always contain a way to terminate. If a loop does not have a way of stopping, it is called an infinite loop. (Not a good thing) An infinite loop continues to repeat until the program is interrupted. See page 139 for a sample of an infinite loop. The corrected code is Program 4-1 Commission.cpp on page 134-135.

The do-while Loop: A Posttest Loop

The do-while loop is a posttest loop. This means that it will perform an iteration at least once before testing the Boolean expression. See the previous page for a diagram that shows the difference between a pretest while loop and a posttest do while loop. (page 140)

ITP 134 – Mrs. EatonChapter 4 Study Guide – 2nd EditionPage 1

Chapter 4 Repetition Structures

ITP 134 – Mrs. EatonChapter 4 Study Guide – 2nd EditionPage 1

Chapter 4 Repetition Structures

4.3 The Increment and Decrement Operators

CONCEPT: To increment a variable means to increase its value, and to decrement a variable means to decrease its value. C++ provides special operators to increment and decrement values.

There are 4 ways to increment a value by 1 in C++: (page 142)

1. num = num +1;

2. num +=1;

3. num++;// called postfix mode

4. ++num;// called prefix mode

There are 4 ways to decrement a value by 1 in C++: (page 142)

1. num = num - 1;

2. num -=1;

3. num--;// called postfix mode

4. --num;// called prefix mode

4.4Count-Controlled Loops: The for Loop

CONCEPT: A count-controlled loop iterates a specific number of times. In C++ the for loop is commonly used as acount-controlled loop. (page 143)

The way a count-controlled loop works is simple. The loop keeps a count of the number of times it iterates, and when the count reaches a specified number, the loop stops.

You need 3 items to set up a for loop:

a) starting counter value

b) test value expression

c) increment or decrement value

In general the for loop syntax is (page 143)

for (j = start; j <= testvalue; j++)

{

statements;

}

Declaring the Counter Variable in the Initialization Expression

You can initialize and declare a counter variable in the for loop. For example simply add the int keyword: (page 147)

for (int count = 0; count <= 5; count++)

Using the Counter Variable in the Body of the Loop

You can use the counter variable in a calculation or other task in a for loop, but you should not change the value inside the loop. (pg 147)

Other Forms of the Update Expression

You can use the increment or decrement value in a for loop for any value you want. See Page 149 for an example of using 2 to increment.

Letting the User Control the Number of Iterations

Sometimes the programmer needs to let the user decide the number of times a loop should repeat. See Program 4-7 UserSquares example on page 151.

4.5 Calculating a Running Total

CONCEPT: A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator. (pg 182)

4.6 Nested Loops

CONCEPT: A loop that is inside another loop is called a nested loop.

Chapter 4 Program Examples

  • Program 4-1 Comissions.cpp (pg 134-135)
  • Program 4-2 PretestWhileLoop.cpp (pg 137-138)
  • Program 4-3 InfiniteLoop.cpp (pg 139) Shows what you should NEVER do.
  • Program 4-4 ForLoop.cpp (pg 145-146)
  • Program 4-5 Squares.cpp (pg 148)
  • Program 4-6 SpeedConverter.cpp (pg 149-150)
  • Program 4-7 UserSquares.cpp (pg 151-152)
  • Program 4-8 SumoOfNumbers.cpp (pg 153-154)
  • Program 4-9 RectangularPattern.cpp (pg 156-158)
  • Program 4-10 TrianglePattern.cpp (pg 158-159)
  • Program 4-11 StairStepPattern.cpp (pg 159-160)

ITP 134 – Mrs. EatonChapter 4 Study Guide – 2nd EditionPage 1

Chapter 4 Repetition Structures

ITP 134 – Mrs. EatonChapter 4 Study Guide – 2nd EditionPage 1