Loop Control Statements II-117
22. What is the output of the following program?
void main()
{
int nm=0;
do
{
--nm;
printf ("%d",nm);
nm++;
}
while(nm>=0);
}
(a) The loop will run infinitely. (b) The program will not be compiled.
(c) Loop will not be executed.
23. What will be the value of s after execution of the following program?
void main()
{
int i=5,a=1;
int s=0;
clrscr();
do
{s=s+a++;
} while (a<=i);
printf ("s=%d",s);
}
(a) s = 15 (b) s = 36 (c) s = 40
24. How many 'while' statements are possible in 'do-while' loop?
(a) 2 (b) 1 (c) 3 (d) None of the above
25. The for (; ;) is equivalent to .
(a) while (false) (b) while (true) (c) while (0) (d) None of the above
26. Which of the following is equivalent of executing 'for' loop without its body?
(a) for(; ;) (b) for(;) (c) for(; ;) ; (d) for( )
27. Which of the following loop is used in C language when programmer is not aware of number of
iterations a loop will follow?
(a) for (b) while (c) do-while
28. The break statement exits .
(a) Only from the inner loop (b) From the innermost loop (c) Only from the inner switch
29. The correct syntax of 'for' loop is
(a) for(i=0;i<n;i++) {----}
(b) for(i=0,j=0;i<n && j<n; i++,j++) {-----}
M06_ITL-ESL4791_02_SE_C06.indd 117 12/22/2012 5:02:22 PM
II-118 Programming Concepts
(c) for(i=0;i++<n;){-----}
(d) All of these
30. Which 'for' loop will terminate itself
(a) for(i=1;i<10;i--) {----} (b) for(i=1,j=1;i++>=1 && j++>=1;) {----}
(c) for(;;) {----} (d) for(i=1,j=1;i||j;i++,j++) {----}
31. The valid 'for' loop is
(a) for(i=0;i<10) (b) for(i>4;i--) (c) for(i=3) (d) for(i=0;;)
32. Which of the following prints 0 1 2 3…..9
(a) void main()
{
int i;
clrscr();
for(i=0;i<10;i++)
printf("%d",i);
getch();
}
(b) void main() (d) All of the above
{
int i,j;
clrscr();
for(i=0,j=0;i<10;i++)
{
printf("%d",j);
j++;
}
getch();
}
33. void main()
{
int i;
clrscr();
for(i=0;++i<=10;)
printf("%d", i);
getch();
}
The output is
(a) 0 1 2,…9 (b) 1 2 3,…9 (c) 1 2 3,…10 (d) Error
34. Which is the correct o/p?
void main()
{
(c) void main()
{
int i,j;
for(i=0,j=0;j/2<10;i++,j=j+2)
printf("%d",i);
getch();
}
M06_ITL-ESL4791_02_SE_C06.indd 118 12/22/2012 5:02:22 PM
Loop Control Statements II-119
int i,j;
clrscr();
for(i=0;i<10;i++)
for(j=0;j<10;i++)
printf("%d",i*j++);
getch();
}
The output is
(a) 0 1 2…..99 (b) 0 1 2…9…..0 1 2…9
(c) 0 1 4 9 16…. 81 (d) 0 1 2 3… 9
Answers
1. (b) 2. (b) 3. (b) 4. (c) 5. (b) 6. (b) 7. (b) 8. (b) 9. (a) 10. (a)
11. (a) 12. (a) 13. (b) 14. (a) 15. (a) 16. (b) 17. (b) 18. (b) 19. (b) 20. (a)
21. (a) 22. (a) 23. (a) 24. (a) 25. (b) 26. (c) 27. (b) 28. (b) 29. (b) 30. (d)
31. (d) 32. (d) 33. (c) 34. (c)
True or False
1. A loop repeatedly executes a block of statements for certain number of times.
2. The loop for(; ;) is a non-working loop.
3. The for(; ;) loop with no arguments can be executed.
4. The loop for (a=1;a<20;a++) will be executed for 20 times.
5. The while(1) is an infinite loop.
6. The loop cannot be nested.
7. Even if the condition is false the do-while loop executes once.
8. The do-while loop must be terminated by semi-colon.
9. The { } defines the block of the statement.
10. In case { } is not defined after the loop statement, the default scope is one statement.
Answers
1. True 2. False 3. True 4. False 5. True 6. False 7. True 8. True 9. True 10. True
Additional Questions
Find the output/s of the following program/s.
1. Program to print numbers and their squares from 1 to 3 using for loop.
void main()
{
int i;
clrscr();
M06_ITL-ESL4791_02_SE_C06.indd 119 12/22/2012 5:02:22 PM
II-120 Programming Concepts
for (i=1;i<=3;i++)
{
printf (" %2d %3d",i,i*i);
}
}
OUTPUT:
1 1
2 4
3 9
2. Progra
m to print numbers and their square roots from 1 to 4 using for loop.
#include <math.h>
void main()
{
int i;
float a;
clrscr();
for (i=1;i<=4;i++)
{
printf (" %d %.3f",i,a=sqrt(i));
}
getche();
}
OUTPUT:
1 1.000
2 1.414
3 1.732
4 2.000
3. Progra
m to print factors of 10 using for and if loop.
void main()
{
int i,m=10;
clrscr();
for(i=1;i<=m;i++)
if(m%i==0)
printf("%d",i);
getche();
}
OUTPUT:
1 2 5 10
M06_ITL-ESL4791_02_SE_C06.indd 120 12/22/2012 5:02:22 PM
Loop Control Statements II-121
4. Program to print the numbers from 1 to 5 in ascending and descending order.
void main()
{
int i=0;
clrscr();
for (;++i<=5;)
printf ("%2d",i);
printf (" ");
for (;--i>=1;)
printf ("%2d",i);
getche();
}
OUTPUT:
1 2 3 4 5
5 4 3 2 1
5.
Program to print even numbers which lies between 1 to 9. Using for loop and nested if..else
with break and continue keywords.
void main()
{
int i=1;
clrscr();
for ( ; ;)
{
if (i==9)
break;
else
if (i%2==0)
{
printf ("%d ",i);
i++;
continue;
}
else
{
i++;
continue;
}
}
getche();
}
OUTPUT:
2 4 6 8
M06_ITL-ESL4791_02_SE_C06.indd 121 12/22/2012 5:02:22 PM
..................Content has been hidden....................

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