Lesson 5: Repetition / Iteration Structure

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

LESSON 5

REPETITION / ITERATION
STRUCTURE
Introduction
Looping
Is a process that execute the same group of
statements for a number of times.

Loop Body
Is a group of statements that executed repeatedly.
3 looping constructs
while loop
do_while loop
for loop
(1) while loop
Best - where dont know ahead of
number of times the loop
General format:
while (expression) while (expression) {
statement; statement1;
statement2;
statement3;
}

If the expression is TRUE, statement(s) will be executed repeatedly until the


condition is FALSE.
(1)while loop
2 ways to control the looping here:
1) Counter-controlled repetition
2) Sentinel-controlled repetition
(1) while loop
- Counter controlled loop

Is to count the number of repetitions


Therefore, it requires:
Name of variable / loop counter
Initial value of the control variable
Increment / decrement by which control variable is modified each time through loop
Final value of control variable

Example:
What is wrong if the following code ?
int x = 10;

while( x < 20 )
printf(%d,x);
(1) while loop
- Sentinel Controlled Loop

Indefinite repetition
Not known in advance how many times the loop will
be executed.
Sentinel value indicates end of data
Is entered after all regular data items have been
supplied to the program.
Sentinel values are used to control repetition when:
1) The precise number of repetitions is NOT known
in advance.
2) The loop includes statements that obtain data
each time the loop is performed.
(1) while loop
- Sentinel Controlled Loop
Example
(2) do-while loop
Tests the loop continuation condition after the loop
statement is performed
The loop body will be executed at least once.
General format:
do {
do statement1;
statement1; statement2;
while (condition); statement3;
statement2; }while (condition);
statement2;

Execution is done by first executing statement, then testing condition.


If condition is FALSE, the do statement terminates and control passes to the next statement in the
program (like executing statement 2).
If condition is TRUE, statement 1 is repeated (or executed) and the process starts over again.
(2) do-while loop
- Counter controlled loop

Example:
(2) do-while loop
- Sentinel controlled loop

Example:
The program is asked to enter a value that is less than 5.
If not, it warns the program user and repeats the loop so that a value within the requested range
can be entered.
Try this..
Write a program that will prompt the user
for 5 marks. Calculate the average and
display the average based on the criteria
below:-
A above 70
B 69 60
C 59 50
D 49 40
F below 40
(3) for loop
To allow you to do something an exact
number of times.
Be able to solve a problem any number of
times with different values each time.
The C for loop contains 4 parts:
1. The value at which the loop starts
2. The condition under the loop is continue.
3. The changes that are to take place for each loop.
4. The loop instructions.

General Format
for (initial-expression; conditional-expression; loop-expression)
{
loop body
}
(3) for loop
Example:

No ; in the for loop expression, it is because the entire combination of


the keyword for loop and statement making up the body of the loop
are one single C statement.
(3) for loop
Compound Statement in for loop
Example:
What is the output of the following
program segment?

for( i = 0; i < 3; ++i)


for( j = 0; j < 3; ++j)
printf(%d\t%d\n,i,j);
NOW try this
for( i = 0; i < 3; ++i) {
for( j = 0; j < 3; ++j)
printf(%d\t%d\n,i,j);
printf(%d\n,i+j);
}
(4) Break & Continue statement
Used to alter the flow of control.
break
Causes immediate exit from that structure.
Program execution continues with the first statement
after the structure.
Common use:
To escape early from a loop or skip the remainder of a
switch structure/loop.
(4) Break & Continue statement
break Example:
(4) Break & Continue statement
continue
Skips the remaining statements in the loop body &
performs the next iteration of the loop.
Example:
Additional Exercise
Write a program to print N even integer
number on the screen.
Write a program to print a rectangle
shape using * character. For example,
if the length is 4 and height is 3, the
shape should be:
****
****
****
Example:
Write a program to allow the user to
enter product number, product unit price
and quantity sold. Calculate and display
the total amount of the product. Your
program need to be able to allow the
user to enter for the next product until
the user choose not to continue.
Change the previous program
If the product unit price is given as for 5
types of products:
Product 123: rm3.50
Product 456: rm2.99
Product 345: rm1.99
Product 890: rm4.99
Product 345: rm6.99

You might also like