Chapter 3
More Flow of Control
3.3
More About C++ Loop
Statements
More About
C++ Loop Statements
A loop is a program construction that repeats a
statement or sequence of statements a number
of times
The body of the loop is the statement(s) repeated
Each repetition of the loop is an iteration
Loop design questions:
What should the loop body be?
How many times should the body be iterated?
Slide 3- 3
while and do-while
An important difference between while and
do-while loops:
A while loop checks the Boolean expression at the
beginning of the loop
A while loop might never be executed!
A do-while loop checks the Boolean expression at
the end of the loop
A do-while loop is always executed at least once
Slide 3- 4
The Increment Operator
We have used the increment operator in
statements such as
number++;
to increase the value of number by one
The increment operator can also be used in
expressions:
int number = 2;
int value_produced = 2 * (number++);
(number++) first returns the value of number (2) to
be multiplied by 2, then increments number to three
Slide 3- 5
number++ vs ++number
(number++) returns the current value of number,
then increments number
An expression using (number++) will use
the value of number BEFORE it is incremented
(++number) increments number first and returns
the new value of number
An expression using (++number) will use
the value of number AFTER it is incremented
Number has the same value after either version!
Slide 3- 6
++ Comparisons
int number = 2;
int value_produced = 2 * (number++);
cout << value_produced << " " << number;
displays 4 3
int number = 2;
int value_produced = 2* (++number);
cout << value_produced << " " number;
displays 6 3
Slide 3- 7
The Decrement Operator
The decrement operator (--) decreases the value
of the variable by one
int number = 8;
int value_produced = number--;
cout << value_produced << " " << number;
displays 8 7
Replacing "number--" with "--number"
displays 7 7
Slide 3- 8
The for-Statement
A for-Statement (for-loop) is another loop
mechanism in C++
Designed for common tasks such as adding
numbers in a given range
Is sometimes more convenient to use than a
while loop
Does not do anything a while loop cannot do
Slide 3- 9
for/while Loop Comparison
sum = 0;
n = 1;
while(n <= 10) // add the numbers 1 - 10
{
sum = sum + n;
n++;
}
sum = 0;
for (n = 1; n <= 10; n++) //add the numbers 1 - 10
sum = sum + n;
Slide 3- 10
For Loop Dissection
The for loop uses the same components as the
while loop in a more compact form
for (n = 1; n <= 10; n++)
Initialization Action Update Action
Boolean Expression
Slide 3- 11
for Loop Alternative
A for loop can also include a variable declaration
in the initialization action
for (int n = 1; n < = 10; n++)
This line means
Create a variable, n, of type int and initialize it with 1
Continue to iterate the body as long as n <= 10
Increment n by one after each iteration
For-loop syntax and while loop comparison
are found in
Display 3.11
Slide 3- 12
Display 3.11
Back Next
Slide 3- 13
for-loop Details
Initialization and update actions of for-loops
often contain more complex expressions
Here are some samples
for (n = 1; n < = 10; n = n + 2)
for(n = 0 ; n > -100 ; n = n -7)
for(double x = pow(y,3.0); x > 2.0; x = sqrt(x) )
Slide 3- 14
The for-loop Body
The body of a for-loop can be
A single statement
A compound statement enclosed in braces
Example:
for(int number = 1; number >= 0; number--)
{
// loop body statements
}
Display 3.13 shows the syntax for a for-loop
with a multi-statement body
Slide 3- 15
The Empty Statement
A semicolon creates a C++ statement
Placing a semicolon after x++ creates the statement
x++;
Placing a semicolon after nothing creates an
empty statement that compiles but does nothing
cout << "Hello" << endl;
;
cout << "Good Bye"<< endl;
Slide 3- 16
Extra Semicolon
Placing a semicolon after the parentheses of a
for loop creates an empty statement as the
body of the loop
Example: for(int count = 1; count <= 10; count++);
cout << "Hello\n";
prints one "Hello", but not as part of the loop!
The empty statement is the body of the loop
cout << "Hello\n"; is not part of the loop body!
Slide 3- 17
Local Variable Standard
ANSI C++ standard requires that a variable
declared in the for-loop initialization section
be local to the block of the for-loop
Find out how your compiler treats these
variables!
If you want your code to be portable, do not
depend on all compilers to treat these variables
as local to the for-loop!
Slide 3- 18
Display 3.1 Back Next
Slide 3- 19
Display 3.2 Back Next
Slide 3- 20
Display 3.3 Back Next
Slide 3- 21
Display 3.4
Back Next
Slide 3- 22
Display 3.5 (1/2)
Back Next
Slide 3- 23
Display 3.5
(2/2) Back Next
Slide 3- 24
Display 3.6 (1/2)
Back Next
Slide 3- 25
Display 3.6
(2/2) Back Next
Slide 3- 26
Display 3.7 (1/2)
Back Next
Slide 3- 27
Display 3.7 (2/2)
Back Next
Slide 3- 28
Display 3.8 (1/2)
Back Next
Slide 3- 29
Display 3.8
(2/2) Back Next
Slide 3- 30
Display 3.9
Back Next
Slide 3- 31
Display 3.10
Back Next
Slide 3- 32
Display 3.11
Back Next
Slide 3- 33
Display 3.12 Back Next
Slide 3- 34
Display 3.13 Back Next
Slide 3- 35
Display 3.14
Back Next
Slide 3- 36
Display 3.15
Back Next
Slide 3- 37