What is looping process ? Distinguish between counter-controlled loops and sentinel-controlled loops.
Answer:
A looping process is
the process which cheek sum condition steps by step.
A looping process in
general would include the following steps_
i. Setting and
initialization of a condition variable.
ii. Execution of the
statements of the loop.
iii. Test for a specified
value of the condition variable for execution of the loop.
iv. Increasing or updating
the condition variable.
Counter-controlled
loop:
When
we know in advance exactly how many times the loop will be executed, we use a
counter controlled loop. We use a countrol variable know as counter. The
counter must be initialized, tested and updated properly for the desired loop
operations. The number of times we want to execute the loop may be a constant
or a variable that is assigned a value. A counter controlled loop is sometimes
called definite repetition loop.
Example:
A while loop is an example of counter controlled loop.
sum
= 0;
n
= 1;
while
(n <= 10)
{
sum
= sum + n*n;
n
= n+ 1;
}
This
is a typical example of counter controlled loop. Here, the loop will be
executed exactly 10 times for n = 1,2,3,......,10.
Sentinel-controlled
loop:
In
a sentinel controlled loop, a special value called a sentinel value is used to
change the loop control expression from true to false. For example, when
reading datd we may indicate the “end of data” by a special value, like-1 and
999. The control variable is called sentinel variable. A sentinel controlled
loop is often called indefinite repetition loop because the number of
repetitions is not known before the loop begins executing.
Example:
The following do....while loop is an example of sentinel controlled loop.
do
{
printf(“Input
a number.\n”);
scanf("%d",
&num);
}
while(num>0);
In
the above example, the loop will be executed till the entered value of the
variable num is not 0 or less then 0. This
is a sentinel controlled loop and here the variable num is a sentinel variable.
OR
Topics
|
Counter
controlled loop
|
Sentinel controlled loop
|
(i)
Number
of execution
|
Previously
known number of execution occurs.
|
Unknown
number of execution occurs.
|
(ii)
Condition
variable
|
Condition
variable is known as counter variable.
|
Condition
variable is known as sentinel variable.
|
(iii)
Value
and limitation of variable
|
The
value of the variable and the limitation of the condition for the variable
both are strict.
|
The
limitation for the condition variable is strict but the value of the variable
varies in this case.
|
(iv) Examples
|
sum
= 0;
n = 1;
while
(n <= 10)
{
sum
= sum + n*n;
n
= n+ 1;
}
|
do
{
printf(“Input
a number.\n”);
scanf("%d",
&num);
}
while(num>0);
|