You are on page 1of 26

23:44:51

Chapter 5
Repetition statements
23:44:46
5.1 while loops

In C++,a while loop is constructed by using a while statement in the


following syntax:
while ( e x p r e s s i o n )
s ta te m e n t ;
Example:
i n t main ( ) {
i n t count ;
count = 1 ; / / i n i t i a l i z e count
while ( count <= 10) {
c o u t << count << ” ” ;
count ++; / / in c re m e n t count
}
r e tu r n 0 ;
}

Higher level programming language


23:44:46
5.2 Interactive while loops

Combining interactive data entry with the repetition capabilities of


the while statement.
Example:
# include <iostream >
# include <iomanip>
u s in g namespace s t d ;
i n t main ( ) {
const i n t MAXNUMS = 4 ;
i n t count ;
double num ;
c o u t << ” \ nThis program w i l l ask you t o e n t e r ”
<< MAXNUMS << ” numbers . \ n ” ;
count = 1 ;

Higher level programming language


23:44:46
5.2 Interactive while loops

while ( count <= MAXNUMS) {


c o u t << ” \ nEnter a number : ” ;
c i n >> num ;
c o u t << ” The number e n te re d i s ” << num ;
count ++;
}
c o u t << e n d l ;
r e tu r n 0 ;
}

Higher level programming language


23:44:45
5.2 Interactive while loops

Example (Practice): Modify the previous example by adding the


numbers entered and display the total.

Higher level programming language


23:44:45
5.2 Interactive while loops

Sentinels:
In computer programming, data values used to signal the start or
end of a data series are called sentinels.
Sentinel values must be selected so as not to conflict with
legitimate data values.

Higher level programming language


23:44:45
5.2 Interactive while loops

break statement:
A break statement forces an immediate break, or exit, from the
switch, while, for, and do-while statements.

Higher level programming language


23:44:45
5.2 Interactive while loops

Example:

while ( count <= 10) {


c o u t << ” E n te r a number : ;
c i n >> num ;
i f (num > 76) {
c o u t << ” You l o s e ! \ n ” ;
break ; / / break o u t o f th e lo o p
} else
c o u t << ” Keep on t r u c k i n g ! \ n ” ;
count ++
}
/ / break jumps t o here

Higher level programming language


23:44:45
5.2 Interactive while loops

continue statement
The continue statement applies only to loops created with while,
do-while, and for statements.
When continue is encountered in a loop, the next iteration of the
loop begins immediately. For while loops, this means execution is
transferred automatically to the top of the loop, and reevaluation of
the tested expression is initiated.

Higher level programming language


23:44:45
5.2 Interactive while loops

Example: Invalid grades are simply ignored, and only valid grades
are added to the total.
while ( count < 30) {
c o u t << ” E n te r a grade : ” ;
c i n >> grade
i f ( grade < 0 | | grade > 100)
continue ;
t o t a l = t o t a l + grade ;
count ++;
}

Higher level programming language


23:44:45
5.3 for loops

In many situations, especially those using a fixed-count condition,


the for statement format is easier to use than its while statement
equivalent. This is the syntax of the for statement:
for ( i n i t i a l i z i n g l i s t ; expression ; a l t e r i n g list )
s ta te m e n t ;

Higher level programming language


23:44:45
5.3 for loops

Example:

# include <iostream >


# include <iomanip>
# include <cmath>
u s in g namespace s t d ;
i n t main ( ) {
const i n t MAXCOUNT = 5 ;
i n t count ;
c o u t << ”NUMBER SQUARE ROOT\n ” ;
c o u t << ”−−−−−− −−−−−−−−−−−\n ” ;
c o u t << s e t i o s f l a g s ( i o s : : showpoint ) ;
f o r ( count = 1 ; count <= MAXCOUNT; count ++)
c o u t << setw ( 4 ) << count
<< setw ( 1 5 ) << s q r t ( double ( count ) ) << e n d l ;
r e tu r n 0 ;
}
Higher level programming language
23:44:24
5.3 for loops

The equivalent while loop to the for loop is as follows:


i n t main ( ) {
const i n t MAXCOUNT = 5 ;
i n t count ;
c o u t << ”NUMBER SQUARE ROOT\n ” ;
c o u t << ”−−−−−− −−−−−−−−−−−\n ” ;
c o u t << s e t i o s f l a g s ( i o s : : showpoint ) ;
count = 1 ;
while ( count <= MAXCOUNT) {
c o u t << setw ( 4 ) << count
<< setw ( 1 5 ) << s q r t ( count ) << e n d l ;
count ++;
}
r e tu r n 0 ;
}

Higher level programming language


23:44:24
5.3 for loops

Remark:
The for statement only ensures that all expressions in the initializing
list are executed once, before evaluation of the tested expression,
and all expressions in the altering list are executed at the end of the
loop, before the tested expression is rechecked.

Higher level programming language


23:44:24
5.3 for loops

Example: The initial is outside the for statement, and the first list
inside the parentheses is left blank.
# include <iostream >
u s in g namespace s t d ;
i n t main ( ) {
i n t count ;
count = 2 ;
/ / i n i t i a l i z e r o u t s i d e th e f o r s ta te m e n t
f o r ( ; count <= 2 0 ; count = count + 2 )
c o u t << count << ” ” ;
r e tu r n 0 ;
}

Higher level programming language


23:44:24
5.3 for loops

Example: Both the initializing list and the altering list are outside
the parentheses.
# include <iostream >
u s in g namespace s t d ;
i n t main ( ) {
i n t count ;
count = 2 ; / / i n i t i a l i z e r o u t s i d e th e f o r lo o p
f o r ( ; count <= 2 0 ; ) {
c o u t << count << ” ” ;
count = count + 2 ; / / a l t e r a t i o n s ta te m e n t
}
r e tu r n 0 ;
}

Higher level programming language


23:44:25
5.3 for loops

Example: All items inside the parentheses, no need for any useful
statement following the parentheses.
# include <iostream >
u s in g namespace s t d ;
i n t main ( ) {
/ / a l l e x p r e s s i o n s i n s i d e f o r ’ s parentheses
i n t count ;
f o r ( count = 2 ; count <= 2 0 ;
c o u t << count << ” ” , count = count + 2 ) ;
r e tu r n 0 ;
}
In this example, the null statement satisfies the syntactical
requirement of one statement to follow fors parentheses.

Higher level programming language


23:44:25
5.3 for loops

Example (Practice): Write a for statement for each of the following


cases:
a Use a counter named i that has an initial value of 1, a final value
of 20, and an increment of 1.
b Use a counter named icount that has an initial value of 1, a final
value of 20, and an increment of 2.
c Use a counter named j that has an initial value of 1, a final value
of 100, and an increment of 5.
d Use a counter named icount that has an initial value of 20, a
final value of 1, and an increment of -1.
e Use a counter named icount that has an initial value of 20, a
final value of 1, and an increment of -2.
f Use a counter named count that has an initial value of 1.0, a
final value of 16.2, and an increment of 0.2.
g Use a counter named xcnt that has an initial value of 20.0, a
final value of 10.0, and an increment of -0.5.
Higher level programming language
23:44:25
5.3 for loops

Example (Practice): Write a C++ program to convert kilometers/hr


to miles/hr. The program should produce a table of 10 conversions,
starting at 60 km/hr and incremented by 5 km/hr. The display
should have appropriate headings and list each km/hr and its
equivalent miles/hr value. Use the relationship that 1 kilometer =
0.6241 miles.

Higher level programming language


23:44:25
5.3 for loops

Example (Practice): Write a C++ program that calculates the


Reynolds number for a pipe having a diameter of 0.1 meters, in
which fluid flows at an average rate of .09 m/s. Your program
should have a fixed-count loop of four and display both the
calculated Reynolds number and the type of fluid flowlaminar,
in-transition, or turbulentfor each fluid listed in the following chart,
using the information provided after the chart. Use the results your
program outputs to fill in the last two columns of this chart:

Higher level programming language


23:44:25
5.3 for loops

The Reynolds number can be calculated by using this formula:


V ×d
Re =
v
Re is the Reynolds number.
V is the average speed of the fluid (ft/sec or m/s).
d is the pipe diameter (ft or m).
v is the kinematic viscosity (ft2 /sec or m2 /sec).
For the determination of flow type, use these facts:
Re < 2000: Fluid flow is smooth (laminar).
2000 ≤ Re ≤ 3000: Fluid flow is in transition.
Re > 3000: Fluid flow is turbulent.

Higher level programming language


23:44:25
5.4 do while loops

Both while and for statements evaluate an expression at the start of


the repetition loop, so they are always used to create pretest loops.
Posttest loops, also referred to as exit-controlled loops, can also be
constructed in C++.
do {
c o u t << ” \ nEnter a p r i c e : ” ; c i n >> p r i c e ;
i f ( abs ( p r i c e SENTINEL ) < 0 .0 0 0 1 )
break ;
s a l e s t a x = RATE ∗ p r i c e ;
c o u t << s e t i o s f l a g s ( i o s : : showpoint )
<< s e t p r e c i s i o n ( 2 )
<< ” The s a l e s t a x i s $ ”<< s a l e s t a x ;
}
while ( p r i c e ! = SENTINEL ) ;

Higher level programming language


23:44:25
5.4 do while loops

Validity checks
The do statement is particularly useful in filtering user-entered input
and providing data validation checks. For example, an operator is
required to enter a valid customer identification number between
1000 and 1999. A number outside this range is to be rejected, and
a new request for a valid number is made.
do {
c o u t << ” \ nEnter an i d e n t i f i c a t i o n number : ” ;
c i n >> id num ;
}
while ( id num < 1000 | | id num > 1 9 9 9 ) ;

Higher level programming language


23:44:26
5.4 do while loops

Above code, a request for an identification number is repeated until


a valid number is entered. It doesnt alert the operator to the cause
of the new request for data or allow premature exit from the loop if a
valid identification number cant be found. The following code is an
alternative for removing the first drawback:
do {
c o u t << ” \ nEnter an i d e n t i f i c a t i o n number : ” ;
c i n >> id num ;
i f ( id num < 1000 | | id num > 1999) {
c o u t << ” An i n v a l i d number was j u s t e n te re d \n ” ;
c o u t << ” Please check th e ID number and r e e n t e r \n ” ;
} else
break ; / / break i f a v a l i d id num was e n te re d
} while ( 1 ) ; / / t h i s e x p r e s s i o n i s always t r u e

Higher level programming language


23:44:26
5.4 do while loops

Example (practice): Write a program that continuously requests a


grade to be entered. If the grade is less than 0 or greater than 100,
your program should print an appropriate message informing the
user that an invalid grade has been entered; else, the grade should
be added to a total. When a grade of 999 is entered, the program
should exit the repetition loop and compute and display the average
of the valid grades entered.

Higher level programming language


23:44:26
5.3 do while loops

Example (practice): Print the decimal, octal, and hexadecimal


values of all characters between the start and stop characters
entered by a user. For example, if the user enters an ’a’ and a ’z’,
the program should print all the characters between a and z and
their respective values. Make sure the second character the user
enters occurs later in the alphabet than the first character. If it does
not, write a loop that asks the user repeatedly for a valid second
character.

Higher level programming language

You might also like