O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1. What is a loop or iterative structure? Why it is necessary in a program?
Ans: Loop or iterative structure is the execution of statements repeatedly until a particular condition
is true. It is necessary in the programming to include loop statements when we want to execute the set
of statements a number of times until some condition is true.
2. Name the two types of loops.
Ans: The loops are of two types as follows.
1. Counter-controlled repetition and
2. Sentinel-controlled repetition.
3. What do you mean by counter-controlled loops?
Ans: This is also called definite repetition actions, because in advance, the number of iterations to be
performed is fixed and defined in the program itself before entering the loop.
4. Give the format and a programming example of counter-controlled loop.
Ans: Format of counter-controlled loop is as follows:
1. Set initially counter with some initial value say 0 or 1.
2. Use 'while' loop to test the count value up to certain value.
3. Execute loop body statements.
4. Update counter.
5. End.
Programming example:
6
Loop Control Statements
int a=0; /* Initial count value of a to 0 */
while(a<=5) /* Test condition */
{
printf("\n Loop statement"); /* Body statement */
a++; /* Update the count */
}
M06_ITL-ESL4791_02_SE_C06.indd 102 12/22/2012 5:02:19 PM
Loop Control Statements II-103
5. Explain the steps for performing counter-controlled repetition.
Ans: Steps in counter-controlled loop are as follows:
1. Loop variable: It is a variable used in the loop.
2. Initialization:It is the first step in which starting/final value is assigned to the loop variable. Each
time the updated value is checked by the loop itself.
3. Incrimination/Decrementation:It is the numerical value added or subtracted to the variable in
each round of the loop. The updated value is compared with the final value and if it is found less
than final value, the steps in the loop are executed.
6. What do you mean by Sentinel-controlled repetition?
Ans: This is also called 'indefinite repetition'; one cannot estimate how many iterations would be
required. In this type loop, termination happens based on certain condition using decision-making
statement.
7. Give a programming example that supports Sentinel-controlled loop.Example is to find 
total odd and even numbers until 0 is pressed using 'while' loop.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,even=0,odd=0;
clrscr();
printf("Enter the number: ");
scanf("%d",&x);
while(x!=0)
{
if(x%2==0)
{
even++;
printf("\nEntered number %d is even ",x);
}
else
{
odd++;
printf("\n Entered number %d is odd ",x);
}
printf("\n\n Enter the number:");
scanf("%d",&x);
}
printf("\n %d %d',even,odd);
getche();
}
M06_ITL-ESL4791_02_SE_C06.indd 103 12/22/2012 5:02:19 PM
II-104 Programming Concepts
Enter the number: 3
Entered number 3 is odd
Enter the number: 5
Entered number 5 is odd
Enter the number: 4
Entered number 4 is even
Enter the number: 8
Entered number 8 is even
Enter the number: 0
2 2
8. List the different types of loop control statement supported by C language.
Ans: C language supports three different types of loop control statements.
1. 'for' loop,
2. 'while' loop and
3. 'do-while' loop.
9. Can we use the words for, do and while as variables in the program?
Ans: No, we cannot use the variable names such as for, do and while, because they are keywords.
Language does not permit to use keywords as variable.
10. Is  it  required  to  terminate  the  loop  control  statements  by  a  punctuation  mark 
semicolon (;)?
Ans: No, loop control statements except 'do-while' loop are terminated by a mark semicolon.
11. What is the syntax of 'for' loop?
Ans: The 'for' loop allows to execute a set of instructions until a certain condition is satisfied.
Condition may be predefined or open-ended. The general syntax of the 'for' loop will be as given in
Table 6.1.
for (initialize counter ;test condition ;re-evaluation parameter)
{
statement ;
statement ;
}
12. Explain the syntax of 'for' loop in brief.
Ans: Syntax of 'for' loop is as follows:
for (initialize counter ;test condition ;re-evaluation parameter)
{
statement ;
statement ;
}
M06_ITL-ESL4791_02_SE_C06.indd 104 12/22/2012 5:02:19 PM

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required