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(" 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(" Entered number %d is even ",x);
}
else
{
odd++;
printf(" Entered number %d is odd ",x);
}
printf(" Enter the number:");
scanf("%d",&x);
}
printf(" %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
Loop Control Statements II-105
Explanation: The 'for' statement contains three expressions, which are separated by semicolons.
Following actions are to be performed in three expressions.
1. The initialization counter sets a loop to an initial value. This statement is executed only once.
2. The test condition is a relational expression that determines the number of iterations desired or it
determines when to exit from the loop. The 'for' loop continues to execute as long as conditional
test is satisfied. When the condition becomes false, the control of the program exits from the body
of the 'for' loop and executes next statement after the body of the loop.
3. The re-evaluation parameter decides how to make changes in the loop (quite often increment or
decrement operations are to be used).
13. Write briefly about the body of the 'for' loop.
Ans: The body of the loop may contain either a single statement or multiple statements. In case there
is only one statement after the 'for' loop, braces may not be necessary. In such a case, the only one
statement is executed until the condition is satisfied. It is a good practice to use braces even for single
statement following the 'for' loop.
14. Draw the flowchart of the execution of 'for' loop.
Ans: The three expressions of 'for' loop are as follows:
for (initialize counter, i.e., expression 1; test condition, i.e., expression 2; re-evaluation parameter,
i.e., expression 3)
{
statement ;
statement ;
}
Flowchart for the above for statement is as follows:
NO
YES
Condition? i.e
Expression 2
Statement (s)
Expression 1
Expression 3
Out of the loop
M06_ITL-ESL4791_02_SE_C06.indd 105 12/22/2012 5:02:20 PM
II-106 Programming Concepts
15. Explain the different ways of specifying the 'for' loop.
Ans: The 'for' loop can be used in different ways. Table 6.1 shows the different ways of using the for loop.
Table 6.1 Different ways to use the for loop
Syntax Output Remarks
(1) for ( ; ; ) Infinite loop No arguments
(2) for (a=0; a<=20; ) Infinite loop 'a' is neither incremented nor decremented.
(3) for (a=0; a<=10; a++)
printf ("%d",a);
Displays value
from 0 to 10
'a' is incremented from 0 to 10. Curly braces are not
necessary. Default scope of 'for' loop is one
statement after
'for' loop.
(4) for (a=10; a>=0; a--) Displays value
from 10 to 0
'a' is decremented from 10 to 0.
16. Write a program to display the square of first five numbers by using 'for' loop.
Ans:
void main()
{
int i;
clrscr();
for (i=1;i<=5;i++)
printf (" umber: %5d its square: %8d",i,i*i);
}
OUTPUT:
Number: 1 its square: 1
Number: 2 its square: 4
Number: 3 its square: 9
Number: 4 its square: 16
Number: 5 its square: 25
Explanation:
1. The value of i is initialized to 1 when the program execution begins.
2. The conditioni<=5 is specified and tested in each iteration. Initially, the condition is true since the
value of i is 1. The statements following the 'for' loop executes.
3. Upon execution of printf() statement, compiler sends control back to the 'for' loop where
the value of i is incremented by one. This is repeated till the value of i is less than or equal to 5.
4. The new updated value of i if exceeds 5, the control exits from the loop.
5. The printf() statement executes as long as the value of i reaches to 5.
17. What is nested 'for' loops?
Ans: Nested means loops within the loop. In 'nested for' loops, one or more 'for'state-
ments are included in the body of the loop. In other words, C allows multiple 'for' loops in nested
forms. The numbers of iterations in this type of structure will be equal to number of iteration in the outer
loop multiply by number of iterations in the inner loop.
M06_ITL-ESL4791_02_SE_C06.indd 106 12/22/2012 5:02:20 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset