Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

DDWC 1573 - PROGRAMMING FUNDAMENTAL

CHAPTER 6: REPETITION STRUCTURES AND


LOOP STATEMENTS

Prepared By:
Mdm. Nik Maria Nik Mahamood

Reference:
Hanly, Koffman, C Problem Solving and Program Design in C, Sixth Edition, Pearson
International Edition. Refer chapter 5 (pg. 242 – pg. 301)
INSPIRING CREATIVE AND INNOVATIVE MINDS
REPETITION STRUCTURES AND LOOP STATEMENTS

6.1 Repetition in Programs


6.2 Counting Loops and the while Statement
6.3 Computing a Sum or a Product in a Loop
6.4 The for Statement
6.5 Conditional Loops: counter - control and sentinel - control
6.6 Loop Design
6.7 The do-while Statement

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.1 REPETITION IN PROGRAMS

• Loop body – the statements that are repeated in the loop

Figure 6.1 Flow Diagram of Loop Choice Process


source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.1 REPETITION IN PROGRAMS

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.1 REPETITION IN PROGRAMS

C IMPLEMENTATION
KIND WHEN USED
STRUCTURES

Can determine before loop while


execution exactly how many loop
Counting loop for
repetitions will be needed to
solve the problem do..while

while
Input of a list of data of any for
Sentinel-controlled loop
length ended by a special value. do..while

Table 6.1 Comparison of Loop Kinds

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

• Counter-controlled Loop – a loop that required number of


iterations can be determined before loop execution begins.

• The loop control variable must be


– Initialized
– Tested
– Updated
for the loop to execute properly

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

• Each step is summarized as:


– Initialization: set to an initial value before the while
statement is reached.
– Testing: tested before the start of each loop repetition
– Updating: updated during each iteration.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

• Syntax:

initialization expression;

while (testing expression){


statement;
update expression;
}

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

Example #1:
int x = 0;
while (x < 4) {
printf(“The value of x is %d”, x);
x = x + 1;
}
Questions:
a) Draw a flowchart for the program fragment above.
b) What output are displayed by the program fragment above?
c) Rewrite the program segment above, using for and do ..while
loop.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

Example #2:

Figure 6.2 Program Fragment with a Loop


source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.2 COUNTING LOOPS AND THE WHILE STATEMENTS

Draw a flowchart for the program fragment in figure 6.2

Figure 6.3 Flowchart for a while Loop


source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.3 COMPUTING A SUM AND A PRODUCT IN A LOOP

Pseudocode:
• Question Start
Write a program that sum = 0
computes and displays bil = 0
the sum of five values while (bil < 5)
entered by user.
begin_while
Display “Enter value:”
Read value
sum = sum + value
bil = bil + 1;
end_while
Stop
INSPIRING CREATIVE AND INNOVATIVE MINDS
6.3 COMPUTING A SUM AND A PRODUCT IN A LOOP

• Loop often accumulate a sum or a product by repeating an


addition or multiplication operation as demonstrated in
Figure 5.4 (Next Slide) - Program To Compute Company
Payroll (pg. 249 – 250)

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.3 COMPUTING A SUM AND A PRODUCT IN A LOOP

Figure 5.4 Program to Compute Company Payroll (Part 1)


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.3 COMPUTING A SUM AND A PRODUCT IN A LOOP

Figure 5.4 Program to Compute Company Payroll (Part 2)


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.3 COMPUTING A SUM AND A PRODUCT IN A LOOP

Figure 5.4 Program to Compute Company Payroll (Part 3)


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.4 THE FOR STATEMENT

• C provides the for statement as another form for implementing


loops.
• Syntax:
for (initialization expression; testing expression; update expression){
statement;
}

• Example:
int x;
for (x = 0; x < 4; x++) {
printf (“The value of x is %d”, x);
}
INSPIRING CREATIVE AND INNOVATIVE MINDS
6.4 THE FOR STATEMENT

Figure 5.5 Using a for Statement in a Counting loop


Source: This figure is taken from Pearson Addison-Wesley, 2004.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.4 THE FOR STATEMENT

Increment and Decrement Operators


• The counting loop that you have seen have all included
assignment expression of the form
Counter = Counter + 1;
Counter = Counter - 1
or
Counter += 1;
Counter -= 1;
• The increment operator ++ means that the value of its
operand in incremented by one.
• The decrement operator -- means that the value of its
operand in decremented by one.
INSPIRING CREATIVE AND INNOVATIVE MINDS
6.4 THE FOR STATEMENT

Increment and Decrement Other than 1


• Example
Counter = Counter + 5;
Counter = Counter - 2
or
Counter += 5;
Counter -= 2;

• See Figure 5.8 (Next Slide) at page 260: Program To Convert


Temperature From Celsius To Fahrenheit.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.4 THE FOR STATEMENT

Figure 5.8 Display a Celsius to Fahrenheit Conversion


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.4 THE FOR STATEMENT

Figure 5.8 Display a Celsius to Fahrenheit Conversion


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

• In many programming situations, we will not be able to


determine the exact number of loop repetitions
before loop execution begin.

• In this case, we still able to write a condition to control


the loop by prompting the user to input a data value.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

• Example #1:
Counter-controlled Loop Based On The User Input
int sum = 0, bil = 0, noOfInput, value;
printf (“Enter number of input:”);
scanf (“%d”, &noOfInput);

while (bil < noOfInput) {


printf (“Enter value: ”);
scanf (“%d”, &value);
sum = sum + value;
bil = bil + 1;
}
printf (“The sum is %d”, sum);

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

Sentinel-Controlled Loops

• The sentinel value - to signal the program to stop reading


and processing new data.

• Sentinel value is an end marker that follows the last item in


a list of data.

• See Figure 5.10: A program that calculates the sum of


collection of exam is a candidate for using a sentinel value.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

• Example #2:
Sentinel-controlled Loop

int sum = 0, value;


printf (“Enter value (-1 to stop): ”);
scanf (“%d”, &value);

while (value != -1) {


sum = sum + value;
printf (“Enter value (-1 to stop): ”);
scanf (“%d”, &value);
}
printf (“The sum is %d”, sum);

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

Figure 5.10 Sentinel-Controlled while Loop


Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.5 CONDITIONAL LOOPS:
COUNTER CONTROL & SENTINEL CONTROL

Using a for statement to implement a Sentinel Loops

/* Accumulate sum of all scores */

printf (“Enter first score (or %d to quit) > ”, SENTINEL);

for(scanf (“%d”, &score);score != SENTINEL; scanf (“%d”, &score)) {


sum += score;
printf (“Enter next score (or %d to quit) > ”, SENTINEL);
}
INSPIRING CREATIVE AND INNOVATIVE MINDS
6.6 LOOP DESIGN

• Problem solving questions for loop design as following table


(based on program the sum of a list of exam scores).

QUESTIONS ANSWER IMPLICATION FOR THE ALGORITHM

What are the Input variable needed score


score
inputs? Value of score must be input once.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.6 LOOP DESIGN

IMPLICATION FOR THE


QUESTIONS ANSWER
ALGORITHM
Values of sum are echoed in
What are the the output.
sum
output? Output variable needed:
sum

Yes.
One repeatedly
Is there any 1. Sum the scores entered Program variable needed
repetition? 2. Get the next score Sentinel value (-99)
3. Checks to see whether
the score is - 99.

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.7 THE DO..WHILE STATEMENT

• Do-while statement evaluate a loop repetition condition at


the end of the loop body.
• Syntax:
initialization expression;
do {
statement;
update expression;
} while (testing expression);

INSPIRING CREATIVE AND INNOVATIVE MINDS


6.7 THE DO..WHILE STATEMENT

• Example:

int x = 0;
do {
printf (“The value of x is %d”, x);
x = x + 1;
} while (x < 4);

INSPIRING CREATIVE AND INNOVATIVE MINDS

You might also like