Ans. : Loop : A loop is a particular area of a program where some executable statements are written which gets execution by testing one or more conditions. So, in looping, a sequence of statements is executed until some conditions for termination are satisfied.
A program loop therefore consists of two segments; the body of the loop and the control statement.
Qus. 02: What are the counter controlled and sentinel controlled loops ?
Ans. : Based on the nature of the control variables and the kind of value assigned to, the loops may be classified into two general categories; counter controlled and sentinel controlled loops.
Counter controlled loops : The type of loops, where the number of the execution is known in advance are termed by the counter controlled loop. That means, in this case, the value of the variable which controls the execution of the loop is previously known. The control variable is known as counter. A counter controlled loop is also 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 : The type of loop where the number of execution of the loop is unknown, is termed by sentinel controlled loop. In this case, the value of the control variable differs within a limitation and the execution can be terminated at any moment as the value of the variable is not controlled by the loop. The control variable in this case is termed by sentinel variable.
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.
Qus. 03: Write down the steps of looping process.
Ans. : Steps of looping process : A looping process, in general, would include the following four 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) Incrementing or updating the condition variable.
The above given steps may come at any order depending on the type of the loop.
Qus. 04: What are the entry controlled and exit controlled loops?
Ans. : Depending on the position of the control statement in the loop, a control structure can be classified into two types; entry controlled and exit controlled. They are described below.
Entry controlled loop : The types of loop where the test condition is stated before the body of the loop, are known as the entry controlled loop. So in the case of an
entry controlled loop, the condition is tested before the execution of the loop. If the test condition is true, then the loop gets the execution, otherwise not. For example, the for loop is an entry controlled loop. In the given figure, the structure of an entry controlled loop is shown.
Exit controlled loop : The types of loop where the test condition is stated at the end of the body of the loop, are know as the exit controlled loops. So, in the case of the exit controlled loops, the body of the loop gets execution without testing the given condition for the first time. Then the condition is tested. If it comes true, then the loop gets another execution and continues till the result of the test condition is not false. For example, the do statement or the do....while loop is an exit controlled loop. The structure of an exit controlled loop is given in the given figure.
Ans. : Comparison among the three loop : The comparison among the three types of loops for loop, while loop and do....while loop is given below.
No. | Topics | For loop | While loop | Do...while loop |
01 | Initialization of condition variable | In the parenthesis of the loop. | Before the loop. | Before the loop or in the body of the loop. |
02 | Test condition | Before the body of the loop. | Before the body of the loop. | After the body of the loop. |
03 | Updating the condition variable | After the first execution. | After the first execution. | After the first execution. |
04 | Type | Entry controlled loop. | Entry controlled loop. | Exit controlled loop. |
05 | Loop variable | Counter. | Counter. | Sentinel & counter |
Also the following comparison of their structures completes the comparison.
For loop | While loop | Do....while loop |
for(n = 1; n <= 10; n++) { ======== ======== } | n = 1; while(n <=10) { ========== ========== n=n+1; } | n = 1; do { ======== ======== n = n + 1; } while(n<=10); |
Qus. 06:Distinguish between entry and exit controlled loops with examples.
Ans. : The differences between the entry and exit controlled loops are as follows.
No. | Topics | Entry controlled loops | Exit controlled loops |
01 | Test condition | Test condition appears at the beginning. | Test condition appears at the end. |
02 | Control variable | Control variable is counter variable. | Control variable is counter & sentinel variable. |
03 | Execution | Each execution occurs by testing condition. | Each execution except the first one occurs by testing condition. |
04 | 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); = = = = = = |
Ans. : The differences between the counter and sentinel controlled loops are as follows.
No. | Topics | Counter controlled loop | Sentinel controlled loop |
01 | Number of execution | Previously known number of execution occurs. | Unknown number of execution occurs. |
02 | Condition variable | Condition variable is known as counter variable. | Condition variable is known as sentinel variable. |
03 | 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. |
04 | 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); = = = = = = |
ক্রেডিট : তন্ময় চক্রবর্তী
1 comments:
Well, you have mentioned the credit. Thats ok, but you could have told once that you are about to copy all my notes here as a book.
Its not a book, its my notes on C programming based on the book of C programming by E. Balaguruswamy.
Ok. Bye.
01. Overview of C
02. Constants, Variables & Data Types
03. Operators & Expressions
04. Managing I/O Operations
05. Decision Making & Branching
06. Decision Making & Looping
07. Arrays
08. User Defined Functions