0% found this document useful (0 votes)
79 views25 pages

Ekt120 Week04 Loop

The document discusses repetition structures or loops in C programming. It introduces while, do-while and for loops. While and for loops repeat until a condition is false, but do-while loops always repeat the code block at least once. The document explains different types of loops like counter-controlled, sentinel-controlled and flag-controlled loops. It provides examples of each loop type and discusses nested loops, which contain a loop within another loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views25 pages

Ekt120 Week04 Loop

The document discusses repetition structures or loops in C programming. It introduces while, do-while and for loops. While and for loops repeat until a condition is false, but do-while loops always repeat the code block at least once. The document explains different types of loops like counter-controlled, sentinel-controlled and flag-controlled loops. It provides examples of each loop type and discusses nested loops, which contain a loop within another loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

EKT 120 SEM II 09/10 1

Week 4 Repetition Structures /


Loops
EKT 120 SEM II 09/10 2
Outline
Introduction
While loops
Three types of while loops
Do-while loops
For loops
Nested loops
EKT 120 SEM II 09/10 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(%d %d %d %d %d, &num1, &num2,
&num3, &num4, &num5);
sum = num1 + num2 + num3 + num4 + num5;
average = sum / 5;
If 100 numbers, 1000 numbers?
EKT 120 SEM II 09/10 4

Repetition (Loop)
Used to control the flow of a program
Loops are basically repetitions or iterations used to
repeat a segment of code
Three statements can be used to implement loops in
C
while statement
do-while statement
for statement
while and for statements are similar in
implementation, but have different syntax
The do-while statement is different - the following
code is executed at least once
EKT 120 SEM II 09/10 5
The while loop structure
The general form of the while statement is:
while (expression)
statement;
To avoid an infinite loop, make sure that
the loops body contains statement (s) that
assure that the exit condition i.e. the
expression in the while statement will
eventually be false.
while loop repeates until condition becomes
false


EKT 120 SEM II 09/10 6
Flowchart of a while statement
EKT 120 SEM II 09/10 7
The while loop structure-cont
There are basically three types of while
loops:
Counter-controlled while loop
Sentinel-controlled while loop
Flag-controlled while loop
EKT 120 SEM II 09/10 8
Counter-Controlled while
Loops
Definite repetition: know how many times loop will execute
Control variable used to count repetitions
Example:
int counter = 1; // declare and initialize
while ( counter <= 10 ) // test condition
{
printf( "%d\n", counter );
++counter; // update
}
Requirement:
1. Declare and initialize
control variable value (or
loop counter)
2. A condition that tests
for the final value of the
control variable (i.e.,
whether looping should
continue)
3. Update control variable
(incr/decr)
EKT 120 SEM II 09/10 9
Counter-Controlled while
Loops
Another example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;

product <= 1000

product = 2 * product

true

false



declare and initialize
test condition
increment
EKT 120 SEM II 09/10 10
Sentinel-Controlled while
Loops
Indefinite repetition
Used when number of repetitions not known
and loop needs to input value repeatedly for
each iteration
Sentinel value indicates "end of data


EKT 120 SEM II 09/10 11
Sentinel-Controlled while
Loops-continued
Example:
int number, count, sum;
sum = 0;
count = 0;

printf(To stop enter -999. Enter positive numbers : " );
scanf(%d, &number);
while (number != -999)
{
sum = sum + number; //find sum of numbers entered
count++; //count how many numbers entered
printf(To stop enter -999. Enter positive numbers : " );
scanf(%d, &number);
}
printf(\nThe sum of %d numbers is %d, count, sum);
Requirement:
1. Read control
variable value
before enter loop
2. A condition that
tests control
variables validity
(i.e., whether
looping should
continue)
3. Read again
control variable
before end of loop

Sentinel value
EKT 120 SEM II 09/10 12
Sentinel-Controlled while
Loops-continued
Another example:
char choice;
printf(Continue? y-yes n-no: );
scanf(%c, &choice);
while ( choice == y)
{ printf(\nEnter grade point:);
scanf(%f, &grade_pt);
if(grade_pt > 2.0)
printf(\nPass);
printf(Continue? y-yes n-no: );
scanf(%c, &choice);
}


read control variable
test condition
read again
EKT 120 SEM II 09/10 13
Flag-Controlled while Loops
Uses a boolean variable to control the loop.
Loop exit when expression is evaluated to false.
bool found = false;

while (!found)
{ printf(Enter number between 1 and 200:);
scanf(%d, &guess);
if ((guess>= 88) &&(guess <=128))
{found = true;
printf(\nBINGO!);}

}

Requirement:
1. Set control variable to
false before loop
2. A condition that tests
control variable. If expr
evaluated to true, loop
continue
3. A decision structure to
test value validity
4. Set control variable to
true to indicate found

Set to false
test condition
decision
structure
Set to true
EKT 120 SEM II 09/10 14
The do-while Repetition
Structure
Similar to the while structure
Condition for repetition is tested after the
body of the loop is performed
All actions are performed at least once
Expression can be written as count-controlled
or sentinel-controlled
Format:
do {
statement;
} while ( condition );



EKT 120 SEM II 09/10 15
Flowchart of a do-while
structure
true

false

action(s)

condition

EKT 120 SEM II 09/10 16
The do-while Repetition
Structure
Example : prints the integers from 1 to 10
int counter = 1;
do {
printf( "%d ", counter );
} while (++counter <= 10);
Another example:
do{
printf(\nEnter grade point:);
scanf(%f, &grade_pt);
if(grade_pt > 2.0)
printf(\nPass);
printf(Continue?y-yes n-no :);
scanf(%c, &choice);
}while ( choice == y)




count-controlled
sentinel-controlled
EKT 120 SEM II 09/10 17
The do-while Repetition
Structure
To avoid an infinite loop, make sure that the loop body contains a
statement that ultimately makes the expression false and assures that
it exits
Another example:
int i = 0;

do
{
printf (%d ,i);
i = i + 5;
} while (i <= 20);

The output is:
0 5 10 15 20
EKT 120 SEM II 09/10 18
The do-while Looping
Structure (Example)
Example: Consider the following two loops






printf (%d ,i); printf (%d ,i);
EKT 120 SEM II 09/10 19
The do-while Looping
Structure (Example)
In (a), the while loop, produces nothing
In (b) the do...while loop, outputs the
number 11
EKT 120 SEM II 09/10 20
The for Repetition Structure
Format when using for loops
for (initialization ; loop continuation test ; increment statement)
Use for loops when already know how
many times to repeat
Example:
for(counter = 1;counter <= 10;counter++)
printf("%d\n",counter);
Prints the integers from 1 to 10




EKT 120 SEM II 09/10 21
The 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
Example: for(int i=0,j=0;j+i<=10;j++,i++)
printf(%d\n,j+i);
EKT 120 SEM II 09/10 22
Flow of a for statement
EKT 120 SEM II 09/10 23
Nested loop
Loop within a loop
Inner loop is performed first
e.g.
for(i=1;i<4;i++){
for(j=1;j<5;j++)
printf(%d, j);
printf(\n);}
Output
1234
1234
1234

EKT 120 SEM II 09/10 24
Nested loop
Another example:
Program finds and prints avg of three test scores, then asks
whether user wants to continue

do
{
for(count=0; count <3;count++)
{ printf(\nEnter test marks: );
scanf(%d, &marks);
total=total+marks;
}
avg=total/count;
printf(\nAverage:%5.2f, avg);
printf(\nContinue?);
scanf(%c, &choice);
}while(choice == y);

EKT 120 SEM II 09/10 25
End Week 4 - Loops
Q & A!

You might also like