0% found this document useful (0 votes)
4 views

Lecture-4

test

Uploaded by

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

Lecture-4

test

Uploaded by

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

23/1/2022

OMAR AL-MUKHTAR UNIVERSITY


COLLEGE OF ENGINEERING

C++ PROGRAMMING LANGUAGE

Lecture #4
Control Structures II (Repetitions)
while Looping (Repetition) Structure
Dr. Ala Alaker
1

Outline
1. Learn about repetition (looping) control structures.

2. Explore how to construct and use while Looping


(Repetition) Structure

3. Learn about various forms of while loops such as :

 sentinel-controlled,

 flag-controlled

Dr. Ala Alaker 1


23/1/2022

This program finds the average number of calories burned in a week.

What if we
want to find the
average number
of calories burned
each day of a
month?

Is this a
better
alternative?

Dr. Ala Alaker 2


23/1/2022

Looping control structures

• C++ has three repetition, or looping, structures


that let you repeat a set of statements over and
over until certain conditions are met.
• The three looping (repetition) structures are:
 while Looping
 for looping
 Do…while Looping

while Looping (Repetition) Structure


• A general syntax of while loop is:

initialization; //initialize the loop control variable (LCV)


while (logical expression) {
Statement
increment; //update the loop control variable (LCV)
}

• while is a reserved word.


• The logical expression can be either a simple or
compound statement. The expression acts as a decision
maker.
• The statement is called the body of the loop.
6

Dr. Ala Alaker 3


23/1/2022

• If the logical expression evaluates to true, the statement


(body of the loop) executes.
• The logical expression is then reevaluated, if it again evaluates
to true, the statement executes again.
• The statement continues to execute (N times) until the
expression is false.
• Loop control variable that serves as a “counter” must be
initialize before the while loop is executed.
• The logical expression checks whether the loop control
variable satisfies certain conditions (counter < N) for the
while loop to continue executing.
• The loop control variable must be updated (reinitialized) in the
body of while loop until the logical expression is evaluated to
false. 7

Consider the following C++ program segment:

declares i to be an loop control


variable and initializes it to 0.
The expression in the while
statement, i <= 20,
The body of the while loop continues
to execute as long as the expression
i <= 20 evaluates to true.
updates the value of i, which
eventually makes i greater than 20
and the expression, i <= 20,
evaluates to false.

Dr. Ala Alaker 4


23/1/2022

following table shows the iterations of the


while loop:

Update
Initialize Expression cout<<i<<“ “;
Iteration LCV
LCV i <= 20 Statements
i=i+5
0 <= 20
1 i=0 0 0+5=5
is true
5 <= 20
2 i=5 10 5 + 5 = 10
is true
10 <= 20
3 i=10 15 10 + 5 = 15
is true
15 <= 20
4 i=15 20 15 + 5 = 20
is true
20<= 20
5 i=20 25 20 + 5 = 25
is true
25 <= 20
6 i=25 The loop terminates
is false 9

• A loop that continues to execute endlessly is called an


infinite loop.
• To avoid an infinite loop, make sure that the logical
expression in the while statement will eventually be
false.

10

Dr. Ala Alaker 5


23/1/2022

Using while loop rewrite the program that finds the average number of
calories burned in a week.

11

Sentinel-Controlled while Loop


• In some cases the number of data (or entries) need to be
read is not known, however the last entry is known.
• The last entry is a special value, called a sentinel, that will
tell the loop to stop.
• In this case, you must read the first item before the while
statement so the test expression will have a valid value to
test.
• If this item does not equal the sentinel, the body of the
while statement executes.
• The while loop continues to execute as long as the program
has not read the sentinel.
12

Dr. Ala Alaker 6


23/1/2022

• In this case, a while loop might look like the following:


cin>> variable; //initialize the loop control variable (LCV)
while (variable != sentinel) {
Statement;
cin>> variable; //update the loop control variable (LCV)
}

Example:
Using Sentinel-Controlled while Loop write a program that compute
and outputs the total number of boxes of cookies sold and the
average number of boxes sold by each volunteer. However, the
program does not know the exact number of Volunteers. Therefore,
the program will prompts the user to enter the value for name as
long as name is not equal to End which is the SENTINEL.
13

14

Dr. Ala Alaker 7


23/1/2022

Flag-Controlled while Loop


• A flag-controlled while loop uses a bool variable to
control the loop.
• Suppose found is a bool variable. The flag-controlled
while loop takes the following form:
found = false; //initialize the loop control variable (LCV)
while (!found) {
.
.
if (expression)
found = true; //update the loop control variable (LCV)
.
.
}

The variable found, which is used to control the execution of the


while loop, is called a flag variable. 15

Example:
Using Flag-Controlled while Loop write a program that asks the
user to enter numbers which are added together. To stop the
addition the user enters a negative number.

16

Dr. Ala Alaker 8

You might also like