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

C++ Repeat Structure

The document discusses repetition control structures in C++, including while, for, and do-while loops. It explains the general syntax for each loop type and how they work. Specifically, it covers initializing and updating loop counter variables, using counters, sentinels, and flags to control loop execution, and the flow of execution for each loop type. Examples are provided to illustrate usage of the different repetition structures.

Uploaded by

Ir Aizuddin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
566 views

C++ Repeat Structure

The document discusses repetition control structures in C++, including while, for, and do-while loops. It explains the general syntax for each loop type and how they work. Specifically, it covers initializing and updating loop counter variables, using counters, sentinels, and flags to control loop execution, and the flow of execution for each loop type. Examples are provided to illustrate usage of the different repetition structures.

Uploaded by

Ir Aizuddin
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Control Structures

3 (Part II: Repetition)

Reference:
D.S. Malik. (2009). C++ Programming: From Problem Analysis to Program Design. 4th
Ed.. Course Technology, Cengage Learning. Boston, USA.
Sem 1 Session 1011 1

OBJECTIVES
In this chapter you will learn:
1. about repetition (looping) control structures.
2. how to construct and use counter-controlled, sentinel-
controlled, and flag-controlled repetition structures.
3. How to examine break and continue statements.

Sem 1 Session 1011 2

1
OUTLINE

1. Repetition structures.
2. while looping (repetition) structures.
3. Designing while loops.
4. for looping (repetition) structures.
5. do…while looping (repetition) structures.
6. break statement.
7. continue statement.

Sem 1 Session 1011 3

1. Repetition Structures
ƒ Assume that a program needs to add five (5)
numbers to find the average.

cin>>n1>>n2>>n3>>4>>n5; //read five numbers


sum=n1+n2+n3+n4+n5; // add the numbers
Average=sum/5; // find the average

ƒ Add 100 numbers to find the average?


ƒ Takes exorbitant amount of space and time, if use
sequential structure to solve the problem.

Sem 1 Session 1011 4

2
1. Repetition Structures

ƒ Repetition structure: repeats a set of


statements several times until certain
conditions are met.

ƒ C++ has three repetition structure:


1. while loop
2. for loop
3. do…while loop

Sem 1 Session 1011 5

2. while Looping Structure

ƒ General form of the while statement:


while (expression)
A
statement;
Meaning: true
expression statement
ƒ while is a reserved word.
ƒ The statement is called
the body of the loop. false
ƒ can be either simple A Flow of
C++ statement or execution of a
compound statement. while loop
Sem 1 Session 1011 6

3
ƒ Consider the following C++ program segment:
i=0; i=0; i=0;
while (i<=20) while (i<=20) while (i<=20);
{ { {
cout << i << “ ”; i += 5; cout << i << “ ”;
i += 5; cout << i << “ ”; i += 5;
} } }
cout << endl; cout << endl; cout << endl;
---------------------------------------------------------------Sample Run
0 5 10 15 20 0 5 10 15 20 25

In this example; the variable i (in Line2) is called the


loop control variable (LCV).
Sem 1 Session 1011 7

3. Designing while loops


// initialize the loop control variables (LCV)

while (expression) // expression test the LCV


{
i=0; //Line 1
: while (i<=20) //Line 2
//update the LCV {
cout << i << “ ”; //Line 3
:
i += 5; //Line 4
} }
cout << endl; //Line 5
Sem 1 Session 1011 8

4
3. Counter-Controlled while loops
ƒ Know how many times certain statements need to
be executed.

ƒ Suppose that a set of statements needs to be


executed N times.
ƒ Set up a counter (initialized to 0 before the while
statement) to track how many times have been read.
ƒ Before executing the body of while statement, the
counter is compared with N.
ƒ Inside the body of the while statement, the value of
counter increments after it reads a new item.

Sem 1 Session 1011 9

3. Counter-Controlled while loops (cont’)

// initialize the loop control variables (LCV)


counter = 0;
while ( counter < N ) // expression test the LCV
{
:
counter++; //update the LCV
:
}

Sem 1 Session 1011 10

5
3. Sentinel-Controlled while loops
ƒ Do not know how many times certain
statements need to be executed, but know
the last entry.

ƒ The last entry is a special value, called a


sentinel.

ƒ The while loop continues to execute a long as


the program has not read the sentinel.

Sem 1 Session 1011 11

3. Sentinel-Controlled while loops (cont’)

// initialize the loop control variables (LCV)


cin >> variable;
while (variable != SENTINEL) // expression test the LCV
{
:
cin >> variable; //update the LCV
:
}

Sem 1 Session 1011 12

6
3. Flag-Controlled while loops

ƒ A flag-controlled while loop uses a bool


variable to control the loop.

Sem 1 Session 1011 13

3. Flag-Controlled while loops (cont’)


// initialize the loop control variables (LCV)
found = false;
while ( !found ) // expression test the LCV
{
:
if (expression) //update the LCV
found = true;
:
}
Sem 1 Session 1011 14

7
4. for Looping Structure

ƒ General form of the for statement:


for (initial sttmt; loop condition; update sttmt)
statement;
The for loop executes as follows:
1. The initial statement executes.
2. The loop condition is evaluated. If the loop condition evaluates
to true
i. Execute the for loop statement
ii. Execute the update statement
3. Repeat step 2 until the loop condition evaluates to false.
Sem 1 Session 1011 15

4. for Looping Structure (cont’)


A

Initial
statement

Loop true
statement
condition
Update
false
statement
A Flow of
execution of a
for loop
Sem 1 Session 1011 16

8
4. for Looping Structure (cont’)

-----------------Sample Run-----------
for (i=0;i<=20; ii += 5
5)
0 5 10 15 20
cout << i << “ ”;
cout << endl;

for (i=20; i>=0; i-=5) 20 15 10 5 0


cout << i << “ ”;
cout << endl;

for (i=0; i<=20; i+=5) ;


cout << i << “ ”;
cout << endl;
-
Sem 1 Session 1011 17

5. do…while Looping Structure

ƒ General form of the do…while statement:


do
statement;
while (expression);
The do…while loop executes as follows:
1. The statement executes first.
2. The expression is evaluated. If the expression evaluates to true.
Otherwise exit from the do…while loop.
i. Repeat step 1.

Sem 1 Session 1011 18

9
5. do…while Looping Structure (cont’)
A

statement

true
expression

false
A Flow of
execution of a
do…while loop
Sem 1 Session 1011 19

ƒ Consider the following C++ program segment:

i=0; i=25;
do do
{ {
cout << i << “ ”; cout << i << “ ”;
i += 5; i += 5;
} while (i<=20); } while (i<=20);
cout << endl; cout << endl;

---------------------------------------------------------------Sample Run
0 5 10 15 20 25

Sem 1 Session 1011 20

10
6. break statement

ƒ When executed in a switch structure, provides


an immediate exit from the switch structure.

ƒ When the break statement executes in a


repetition structure, it immediately exits from
the loop structure.

Sem 1 Session 1011 21

6. break statement (cont’)

ƒ Typically used for two purposes:

1. To exit early from a loop.


2. To skip the remainder of the switch
structure.

Sem 1 Session 1011 22

11
sum=0;
6. break statement (cont’)
isNegative=false;

cin >> num;


while (cin &&) !isNegative)
{
if (num<0) ƒƒ Can Can eliminate
eliminate thethe use
use of
of
{ certain (flag)
certain (flag) variables.
variables.
cout<<“Negative number found in the data.” << endl;
isNegative=true;
break;
8
}
3
else 2
{ 0
sum+=num; 1
cin>>num; -10
Negative number found in the data.
}
Press any key to continue
} Sem 1 Session 1011 23

7. continue statement

ƒ When the continue statement executes in a


repetition structure, it skips the remaining
statements in the loop and proceeds with the
next iteration of the loop.

Sem 1 Session 1011 24

12
7. continue statement (cont’)

ƒ In a while and do…while structure, the


expression is evaluated immediately after the
continue statement.

ƒ However, the update statement may not


execute.

Sem 1 Session 1011 25

7. continue statement (cont’)

ƒ In a for structure, the update statement is


executed after the continue statement.

ƒ Then the loop condition executes.

Sem 1 Session 1011 26

13
sum=0;

cin >> num;


6. break statement (cont’)
while (cin)
{
if (num<0)
{
cout<<“Negative number found in the data.” << endl;
cin >> num; 3
continue; -4
Negative number found in the data.
} 9
else 0
{ -1
Negative number found in the data.
sum+=num; 77
cin>>num; 21
} 03
11
} Sem 1 Session 1011 27

14

You might also like