Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COMPUTER PROGRAMMING
Control Structure:
Repetition
Rusnida Romli
Recaps... Control Structure
2
Today’s Outline
• Introduction to Repetition Structure
(Loops)
• Type of loops
– while loops
– do-while loops
– for loops
• Nested loops
3
Why Need Loops ?
• Suppose we want to add five numbers and
find the average.
• From what you have learned so far, you could
proceed as follows
scanf(“%f %f %f %f %f”, &fNum1, &fNum2,
&fNum3, &fNum4, &fNum5);
fSum = fNum1 + fNum2 + fNum3 + fNum4 + fNum5;
fAverage = fNum / 5;
4
Repetition Structures (Loop)
6
Flowchart of a while statement
7
Types of while Loops
• There are basically three types of while
loops:
– Counter-controlled while loop
– Sentinel-controlled while loop
– Flag-controlled while loop
8
Counter-Controlled while Loops
#include <stdio.h>
int main()
{ Declare and initialize control variable
value (or loop counter)
int iCounter = 1;
test condition A condition that tests for
the final value of the control variable (i.e.,
while ( iCounter <= 10 )
whether looping should continue)
{
printf( "%d\n", iCounter );
10
Example : Counter-Controlled while
#include <stdio.h>
int main()
{
1
2
int iCounter = 1;
3
4
while ( iCounter <= 10 )
5
{
6
printf( "%d\n", iCounter );
7
8
++iCounter;
9
}
10
return 0;
}
11
Example : Counter-Controlled while
• Adding integers from 1 to 100
true
iCounter <= 100 iSum = iSum + iCounter
false
++iCounter; increment
} 12
Example : Counter-Controlled while
• Adding integers from 1 to 100
true
iCounter <= 100 iSum = iSum + iCounter
false
Adding 1 to 0 = 1
Adding 2 to 1 = 3
int iCount = 1; declare and initialize
Adding 3 to 3 = 6
int iSum=0; …
while (iCount <= 100 ){ … test condition
printf(“Adding %d to %d ”, iCount,iSum);
…
iSum = iSum + iCount; Adding 100 to 4950 = 5050
printf(“ = %d”, iSum);
++iCounter; increment
} 13
Sentinel-Controlled while Loops
19
Example : Flag-Controlled while
Adding multiple numbers
bool proceed; // used to control the while loop
int sum; // the sum of the numbers read
int number; // the number read
printf(“Sum is %d \n”,sum); 20
Example : Flag-Controlled while
Adding multiple numbers
bool proceed; // used to control the while loop
int sum; // the sum of the numbers read
int number; // the number read
22
Flowchart of a do-while loop
action(s)
true
condition
false
23
Example: Counter-Controlled do-while
• Adding integer from 1 to 10
int iCounter = 1;
int iSum=0;
do {
printf(“Adding %d to %d =",iCounter,iSum );
iSum=iSum+iCounter;
printf(“ %d\n”,iSum);
24
Example: Counter-Controlled do-while
• Adding integer from 1 to 10 Adding 1 to 0 = 1
Adding 2 to 1 = 3
int iCounter = 1; Adding 3 to 3 = 6
…
int iSum=0; …
…
do { Adding 10 to 45 = 55
printf(“Adding %d to %d =",iCounter,iSum );
iSum=iSum+iCounter;
printf(“ %d\n”,iSum);
25
Example: Sentinel-Controlled while
Evaluating grade points
do
{
printf(“\nEnter grade point: ”);
scanf(“%f”, &fGradePt);
char cChoice;
float fGradePt=0;
do
{
printf(“\nEnter grade point: ”);
scanf(“%f”, &fGradePt);
30
3. for Repetition Structure
• for loops can usually be rewritten as while loops:
initialization;
while ( loop continuation test ) {
statement;
increment statement;
}
• Initialization and increment can be comma-separated lists
int iVar1, iVar2;
for(iVar1=0,iVar2=0; iVar2+iVar1<=10; iVar2+
+,iVar1++)
printf(“%d\n”,iVar2+iVar1);
31
Flowchart of for statement
32
Nested loop
• Loop within a loop
• Inner loop is performed first
• Example 1:
fAvg=iTotal/iCount;
printf(“\nAverage:%5.2f”, fAvg);
printf(“\nContinue?”);
scanf(“%c”, &cChoice);
}while(cChoice == ‘y’); 34
Nested loop
• Example 2: Program finds and prints average of
three test scores, then asks if user wants to
continue
do
{
for(iCount=0; iCount <3;iCount++)
{ printf(“\nEnter test marks: ”);
scanf(“%d”, &iMarks); Enter test marks : 78
iTotal=iTotal+iMarks; Enter test marks : 85
} Enter test marks : 82
Average : 81.67
fAvg=iTotal/iCount; Continue? y
printf(“\nAverage:%5.2f”, fAvg);
Enter test marks : 65
printf(“\nContinue?\n”); Enter test marks : 73
scanf(“%c”, &cChoice); Enter test marks : 68
}while(cChoice == ‘y’); Average : 68.67
Continue? n 35
Exercise 1
• Write a C code segment using the while loop
to find the sum of the first 99 positive integers
1,2,3…99.
Display the resulting sum.
Declare and initialize variables used.
36
ANSWER: Exercise 1
#include <stdio.h>
void main(){
int counter, sum; // declare variables
counter = 1; // initialization
sum = 0;
while (counter < 100){ // iterate 99 times
sum = sum + counter; // adding values to
sum
++counter; // increment the counter
}
printf("The sum is %d “, sum ); // Display the sum
}
37
ANSWER: Exercise 1
The sum is 495
#include <stdio.h>
void main(){
int counter, sum; // declare variables
counter = 1; // initialization
sum = 0;
while (counter < 100){ // iterate 99 times
sum = sum + counter; //adding values to sum
++counter; // increment the counter
}
printf("The sum is %d “, sum ); // Display the sum
}
38
Exercise 2
• Write a C program to read characters from the
keyboard until a ‘q’ character is read.
Use a sentinel-controlled while loop to find
the number of nonblank characters read from
the keyboard
39
ANSWER: Exercise 2
#include <stdio.h>
void main(){
char letter; //declaration of letters read
int counter=0; //declaration and initialization
scanf(“%s”,&letter); // read the first letter
while ( letter != 'q'){ // loop until 'q' is read
printf(“%s\n”,letter);
++counter; // increment letter counter
scanf(“%s”,&letter); // read next letter
}
printf("The number of letters read is %d\n“,
counter);
}
40
ANSWER: Exercise
A
B
2
C
D
#include <stdio.h> E
void main(){ q
char letter; The number
//declaration ofof letters
letter is 5 read
int counter=0; //declaration and initialization
scanf(“%c”,&letter); // read the first letter
while ( letter != 'q'){ // loop until 'q' is read
printf(“%c\n”,letter);
++counter; // increment letter counter
scanf(“%c”,&letter); // read next letter
}
printf("The number of letters read is %d\n“,
counter);
}
41
Exercise 3
• Write a program that utilizes looping to
print the numbers from 1 to 10 side-by-
side on the same line with 3 spaces
between each number
42
ANSWER: Exercise 3
#include<stdio.h>
int main()
{
int i=1; //initialize i
43
Exercise 4
• Finding Error
for ( x = .000001; x <= .0001; x += .000001 )
printf( "%.7f\n", x );
ANSWER:
Floating point numbers should never be used in loops
due to imprecision. This imprecision often causes
infinite loops to occur. To correct this, an integer
variable should be used in the for loop.
44
Exercise 5
What the following program do?
#include<stdio.h>
int main()
{
int x,y,i,j; //declare variables
for(i=1;i<=y;i++){
for(j=1;j<=x;j++)
{
printf(“@”); // display output @
} //end inner for
printf(“\n”);
}
return 0; //indicate successful termination
} 45
ANSWER : Exercise 5
46
How about this?
...
for(i=0;i<5;i++)
@
{ for(j=0;j<=i;j++) @@
printf(“@”); @@@
printf(“\n”);
@@@@
}
@@@@@
….
Exercise 6
48
ANSWER : Exercise 6
49
Q & A!
50