Professional Documents
Culture Documents
C++ Programming: Repetition Structure
C++ Programming: Repetition Structure
C++ Programming: Repetition Structure
C++ PROGRAMMING
1 Chapter 5
REPETITION STRUCTURE
CHALLENGE
Write a program to print exactly 1000 lines of the
message:
USCS20/Hanin
“I will score A for this subject.”
//for loop
for (int i=0; i<1000; i++)
{
cout << “I will score A for this subject” << endl;
}
//while loop
int count = 0;
while (count < 1000)
{
cout << “I will score A for this subject” << endl; 2
}
OBJECTIVES
Basic Loop Structures
while Loops
USCS20/Aizat
Interactive while Loops
for loops
Nested Loops
do while Loops
3
BASIC LOOP STRUCTURE
It is convenient if you could type repeating
instructions only once in a program.
USCS20/Hanin
This method is available by using repeating sections
of code.
4
ELEMENTS OF REPETITION STRUCTURE
1)Repetition statement :– while, for, do while
USCS20/Aizat
2) Condition – pretest and posttest loops
4) Loop Termination
5
CONDITION: PRE-TEST LOOP
Pretest loop: condition is
tested first; if false,
statements in the loop body
USCS20/Aizat
are never executed
while and for loops are
pretest loops
6
CONDITION: POST-TEST LOOP
Posttest loop: condition is
tested after the loop body
statements are executed;
USCS20/Aizat
loop body always executes
at least once
do while is a posttest
loop
7
LOOP TERMINATION
Fixed-count loop: loop is processed for a fixed number
of repetitions
USCS20/Aizat
Variable-condition loop: number of repetitions depends
on the value of a variable
8
WHILE LOOPS
Statements following the expressions are executed as
long as the expression condition remains true (evaluates
to a non-zero value)
USCS20/Hanin
Syntax: while (expression)
statement;
sum = 0;
n = 1; //initial value
while(n <= 10) // expression
{
sum = sum + n;
n++; //altering list 9
}
USCS20/Aizat
10
USCS20/Aizat
11
EXERCISE!
Write a C++ program that use while loop to display in
decreasing order start from number 100 to 1.
USCS20/Aizat
12
CHALLENGE
Write a while loop to count the number of even numbers
from 1 to 78
USCS20/Hanin
13
INTERACTIVE WHILE
Combining interactive data entry with the while
statement provides for repetitive entry and accumulation
of totals
USCS20/Aizat
Conclusion: cin>> in while loop
14
USCS20/Aizat
15
INTERACTIVE WHILE LOOPS:
SENTINEL
Previously have been examples of fixed-count loops –
counter is used to control the number of loop iterations.
USCS20/Aizat
Sentinel: a data value used to signal either the start or
end of a data series
Use a sentinel when you don’t know how many values
need to be entered
16
USCS20/Aizat
17
USE YOUR BRAIN POWER!
Write a program that will accept a user-entered number. Input
as many positive number as user want and enter 0 in order to
stop the iteration. Calculate and print the average of the
USCS20/Aizat
number entered.
18
FOR LOOPS
for statement: a loop with a fixed count
condition that handles alteration of the condition
USCS20/Aizat
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 19
Relational Expression
FOR LOOPS
Initializing list: sets the starting value of a counter
Expression: contains the maximum or minimum
USCS20/Aizat
value the counter can have; determines when the
loop is finished
Altering list: provides the increment or decrement
value that is added or subtracted from the counter in
each iteration of the loop
20
FOR LOOPS (CONT..)
Sample
USCS20/Aizat
int i;
for (i=1; i<=10; i++)
{ cout<<i<< “ “ ;
}
Output:
1 2 3 4 5 6 7 8 9 10
21
FOR/WHILE LOOP COMPARISON
sum = 0;
n = 1;
while(n <= 10) // add the numbers 1 - 10
USCS20/Aizat
{
sum = sum + n;
n++;
}
sum = 0;
for (n = 1; n <= 10; n++) //add the numbers 1 - 10
sum = sum + n;
22
EXERCISE!
Using a for loop, write a C++ statement to display:
USCS20/Aizat
2 5 8 11 14 17 20
23
NESTED LOOPS
Nested loop: a loop contained within
another loop
USCS20/Aizat
All statements of the inner loop must be
completely contained within the outer loop;
no overlap allowed
Different variables must be used to control
each loop
For each single iteration of the outer loop, the
inner loop runs through all of its iterations
24
NESTED LOOPS (CONT..)
USCS20/Aizat
25
NESTED LOOPS (CONTINUED)
26
NESTED LOOPS (CONT..)
Other sample of program
No. of rows
for ( i=1; i<=4; i++)
USCS20/Aizat
{ No. of columns
for (j=1; j<=3; j++)
cout<<j<< “ “;
cout<<endl;
} Output:
1 2 3
1 2 3
1 2 3
1 2 3
27
NESTED LOOP EXERCISE!
Write a program that will read ten students’ scores for 4
subjects they are currently taking for this semester.
Finally calculate the average score for each student.
USCS20/Aizat
28
CHALLENGE
Write a nested loop to display the following output:
*****
USCS20/Hanin
****
***
**
*
29
DO WHILE LOOPS
do while loop is a posttest loop
Loop continues while the condition is true
Condition is tested at the end of the loop
USCS20/Aizat
Syntax:
do
statement;
while (expression);
All statements are executed at least once in a posttest
loop
30
DO WHILE LOOPS: VALIDITY CHECKS
Useful in filtering user-entered input and providing data
validation checks
USCS20/Aizat
Can enhance with if-else statement
31
CHOOSING CORRECT LOOP
STATEMENT?
Choosing a for loop
for-loops are typically selected when doing
numeric calculations, especially when using
USCS20/Aizat
a variable changed by equal amounts each
time the loop iterates
Choosing a while loop
A while-loop is typically used when a for-loop is not appropriate
When there are circumstances for which the loop body should
not be executed at all
Choosing a do- while loop
A do-while-loop is typically used when a for-loop is not
appropriate.
When the loop body must be executed at least once 32
COMMON PROGRAMMING ERRORS
Using the assignment operator (=) instead of
the equality comparison operator (==) in the
USCS20/Aizat
condition expression
Placing a semicolon at the end of the for
clause, which produces a null loop body
Using commas instead of semicolons to
separate items in the for statement
Omitting the final semicolon in a do
statement 33