0% found this document useful (0 votes)
75 views2 pages

ITP 134 C++ Study Guide: Chapter 4 Repetition Structures

The document summarizes key concepts from Chapter 4 of the textbook, which covers repetition structures (loops) in C++. It discusses the three main types of loops: 1) condition-controlled loops like while and do-while that repeat based on a boolean condition, and 2) count-controlled loops like for loops that repeat a specific number of times. It provides examples of initializing counter variables, increment/decrement operators, nested loops, and using loops to calculate running totals. The chapter introduces common loop patterns like summing values and printing patterns and includes sample code from various programs using different loop types.

Uploaded by

Asif Javid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views2 pages

ITP 134 C++ Study Guide: Chapter 4 Repetition Structures

The document summarizes key concepts from Chapter 4 of the textbook, which covers repetition structures (loops) in C++. It discusses the three main types of loops: 1) condition-controlled loops like while and do-while that repeat based on a boolean condition, and 2) count-controlled loops like for loops that repeat a specific number of times. It provides examples of initializing counter variables, increment/decrement operators, nested loops, and using loops to calculate running totals. The chapter introduces common loop patterns like summing values and printing patterns and includes sample code from various programs using different loop types.

Uploaded by

Asif Javid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

Control Structures 4.2 Condition-Controlled Loops: The


CONCEPT: Control structures affect the order in which while Loop and the do-while loop
statements execute. There are 3 main types of control CONCEPT: Both the while and do-while loops cause a
structures, sequence, selection, and repetition. (pg 87) statement or a set of statements to repeat as long as
1. Sequence – set of statements that execute in the condition is true.
the order that they appear
2. Selection (also called decision) – structure used
to create a program with more than one path of The while Loop
execution (pg 89) The while loops gets its name from the way it works.
3. Repetition (also called loop or iteration ) – While a Boolean expression is true, do some task. (page
structure used to repeat the same set of 133) See the previous page for a diagram that shows
statements a number of times (pg 89) the difference between a pretest while loop and a
posttest do while loop.
Combine Control Structures (Not in book, from
CSC 200 class) The while Loop is a Pretest Loop
 Each control structure has a single entry and a The while loop is known as a pretest loop, which means
single exit point. it tests the expression before performing the first
 Two ways to combine control structures, stack iteration. (page 136)
or nest.
Infinite Loops
Loops should always contain a way to terminate. If a
4.1 Introduction to Repetition Structures
loop does not have a way of stopping, it is called an
CONCEPT: A repetition structure causes a statement or
infinite loop. (Not a good thing) An infinite loop
set of statements to execute repeatedly. (pg 131)
continues to repeat until the program is interrupted.
A repetition structure is also known as a loop. See page 139 for a sample of an infinite loop. The
In this chapter we will look at 2 categories of loops: corrected code is Program 4-1 Commission.cpp on page
134-135.
1. Condition-controlled loop
While and do-while statements are condition- The do-while Loop: A Posttest Loop
controlled loops because they use a true/false The do-while loop is a posttest loop. This means that it
condition to control the number of times it will perform an iteration at least once before testing the
repeats. Boolean expression. See the previous page for a
diagram that shows the difference between a pretest
2. Count controlled loops
while loop and a posttest do while loop. (page 140)
A for loop is a count-controlled loop because it
repeats a specific number of times.

ITP 134 – Mrs. Eaton Chapter 4 Study Guide – 2nd Edition Page 1
Chapter 4 Repetition Structures

4.3 The Increment and Decrement


Operators Using the Counter Variable in the Body of the
CONCEPT: To increment a variable means to increase Loop
its value, and to decrement a variable means to You can use the counter variable in a calculation or
decrease its value. C++ provides special operators to other task in a for loop, but you should not change the
increment and decrement values. value inside the loop. (pg 147)
There are 4 ways to increment a value by 1 in C++:
(page 142) Other Forms of the Update Expression
You can use the increment or decrement value in a for
1. num = num + 1; loop for any value you want. See Page 149 for an
2. num +=1; example of using 2 to increment.
3. num++; // called postfix mode
4. ++num; // called prefix mode Letting the User Control the Number of Iterations
There are 4 ways to decrement a value by 1 in C++: Sometimes the programmer needs to let the user
(page 142) decide the number of times a loop should repeat. See
Program 4-7 UserSquares example on page 151.
1. num = num - 1;
2. num -=1; 4.5 Calculating a Running Total
3. num--; // called postfix mode CONCEPT: A running total is a sum of numbers that
4. --num; // called prefix mode
accumulates with each iteration of a loop. The variable
used to keep the running total is called an accumulator.
4.4 Count-Controlled Loops: The for Loop
(pg 182)
CONCEPT: A count-controlled loop iterates a specific
number of times. In C++ the for loop is commonly used
4.6 Nested Loops
as a count-controlled loop. (page 143)
CONCEPT: A loop that is inside another loop is called a
The way a count-controlled loop works is simple. The nested loop.
loop keeps a count of the number of times it iterates,
and when the count reaches a specified number, the Chapter 4 Program Examples
loop stops.  Program 4-1 Comissions.cpp (pg 134-135)
 Program 4-2 PretestWhileLoop.cpp (pg 137-
You need 3 items to set up a for loop:
138)
a) starting counter value
 Program 4-3 InfiniteLoop.cpp (pg 139) Shows
b) test value expression
what you should NEVER do.
c) increment or decrement value
 Program 4-4 ForLoop.cpp (pg 145-146)
In general the for loop syntax is (page 143)
for (j = start; j <= testvalue; j++)  Program 4-5 Squares.cpp (pg 148)
{  Program 4-6 SpeedConverter.cpp (pg 149-150)
statements;  Program 4-7 UserSquares.cpp (pg 151-152)
}  Program 4-8 SumoOfNumbers.cpp (pg 153-154)
 Program 4-9 RectangularPattern.cpp (pg 156-
Declaring the Counter Variable in the 158)
Initialization Expression  Program 4-10 TrianglePattern.cpp (pg 158-159)
You can initialize and declare a counter variable in the  Program 4-11 StairStepPattern.cpp (pg 159-160)
for loop. For example simply add the int keyword:
(page 147)
for (int count = 0; count <= 5; count+
+)

ITP 134 – Mrs. Eaton Chapter 4 Study Guide – 2nd Edition Page 2

You might also like