Loop Control Statements II-107
18. Write a program to illustrate an example of 'nested for' loops.
Ans:
Explanation: Here, in the above example, for each value of i, the inner loop's j
value varies from 1 to 2.
Outer loop executes three times whereas the inner loop executes six times. The inner loops terminates
when the value of j = 2 and the outer loop terminates when the value of i = 3. The total number of
iterations = outer loop iterations (3) * inner loop iterations (2) = 6.
19. Is it possible to create 'for' loop that never executes?
Ans: Yes, we can create 'for' loop that never executes when initial condition itself is not true. In
such a case, control will not be passed inside 'for' loop even once.
20. What is the syntax of 'while' loop?
Ans: The format of 'while'loop is as follows:
Syntax:
while (test condition)
{
body of the loop
}
The test condition is indicated at the top and it tests the value of the expression before processing the
body of the loop. The test condition may be any expression. The loop statements will be executed till the
condition is true, i.e., the test condition is evaluated and if the condition is true, then the body of the loop
is executed. When the condition becomes false, the execution will be out of the loop.
# include <stdio.h>
# include <conio.h>
void main()
{
int i,j;
clrscr();
for (i=1;i<=3;i++) /* outer loop */
{
for (j=1;j<=2;j++) /* inner loop */
printf (" i*j: %d",i*j);
}
}
OUTPUT:
i*j = 1
i*j = 2
i*j = 2
i*j = 4
i*j = 3
i*j = 6
M06_ITL-ESL4791_02_SE_C06.indd 107 12/22/2012 5:02:20 PM
II-108 Programming Concepts
21. Is flowchart number correct?
Ans:
1. The test condition is evaluated and if it is true, the body of the loop is executed.
2. On execution of the body, test condition is repetitively checked and if it is true, the body is executed.
3. When the condition is false, the control is transferred out of the loop.
Next
statement
Test condition
?
Body statements
Update the
initialize value
Initialize
F
T
Entry
(a) Flowchart number 1
22. Writea program to printthe string'You have learnt C program' 9 times using 'while'loop.
void main()
{
int x=1;
while (x<10)
{
printf (" You have learnt C program");
x++;
}
}
OUTPUT:
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
M06_ITL-ESL4791_02_SE_C06.indd 108 12/22/2012 5:02:21 PM
Loop Control Statements II-109
Explanation: The parentheses after 'while' contains a condition. As long as condition remains true,
all the statements within the body of the loop get executed repeatedly. The variable x is initialized to 1.
The compiler checks the condition and after satisfying it, the body of the loop is executed. The control
then goes to the 'while' loop. Next time, the value of x is incremented by 1. Now, x is 2 again it
satisfies the condition and the body of the loop gets executed. This process is continued till the value of
x reaches to 9. When the condition becomes false (after 9), the control passes to the first statement that
follows the body of the 'while' loop and the program is now terminated.
23. Write and explain the syntax of 'do-while' loop.
Ans: Syntax of 'do-while' loop is as follows:
do
{
statement/s;
}
while (condition);
In 'do-while' loop, the condition is checked at the end of the loop. The 'do-while' loop will
execute at least one time even if the condition is false initially. The 'do-while' loop executes as long
as the condition becomes true.
24. What are characteristics of a 'do-while' loop?
Ans: In the 'do-while' loop, the test condition is mentioned at the bottom of the 'do-while'
loop. The body statement(s) execute(s) once even when condition is false. Braces are essential even
when a single statement exists.
25. Write a program to find the cubes of 1 to 10 numbers using 'do-while' loop.
#include 'math.h'
void main()
{
int y,x=1;
clrscr();
printf (" Print the numbers and their cubes");
printf (" ");
do
{
y=pow(x,3);
printf ("%4d %27d ",x,y);
x++;
}
while (x<=10);
}
M06_ITL-ESL4791_02_SE_C06.indd 109 12/22/2012 5:02:21 PM
II-110 Programming Concepts
OUTPUT:
Print the numbers and their cubes
1 1
2 8
3 27
4 64
5 125
6 216
7 343
8 512
9 729
10 1000
Explanation: Here, the mathematical function pow (x, 3) is used. Its meaning is to calculate the third
power of x. With this function, we get the value of y = x
3
. For use of the pow () function, we have to
include math.h header file.
26. What is the difference between 'while' and 'do-while' loops?
Ans:
S. No. 'while' loop 'do-while' loop
1 Condition is specified at the top. Condition is mentioned at the bottom.
2 Body statement(s) is/are executed
when condition is satisfied.
Body statement(s) execute(s) even when condition is false.
3 No brackets for a single statement. Brackets are essential even when a single statement exits.
4 It is entry controlled loop. It is exit controlled loop.
27. What happens if you create a loop that never ends?
Ans: If we create a loop that never ends, then the execution will not stop and may be the program will
close automatically due to crashing of system stack.
28. Is it possible to nest 'while' loop within 'for' loops?
Ans: Yes, we can nest 'while' loop within 'for' loop, because there is no limitation on the state-
ments that can be written inside the block of loop statement, we can write any statements enclosed in
loop block.
29. How do you choose between 'while' and 'for' loops?
Ans: When we do not want to initialize any variables at the start of loop statement or we do not want
to perform any repetitive operation after each execution of loop statement, the 'while' statement is
used; otherwise, we prefer to use 'for' loop statement. Both of these statements can be used alter-
nately with very less overhead.
30. What is the difference between (!0) and (!1)?
Ans: The (!0) condition is the condition that will never become false and (!1) is the condition that is
always false, which is equivalent to Boolean values 1 and 0, respectively.
M06_ITL-ESL4791_02_SE_C06.indd 110 12/22/2012 5:02:21 PM
Loop Control Statements II-111
31. What are the values of NULL and !NULL?
Ans: The NULL value is used to check for the end of the string and !NULL can be used as condition
that will be true until the end of the string does not occurs.
32. Is it possible to use multiple 'while' statements with 'do' statement?
Ans: We cannot use multiple 'while' statements with a single 'do' statement; every 'do' state-
ment can be paired with one and only one 'while' statement in the 'do-while' loop.
33. What happens when the condition within the 'while' loop is not specified?
Ans: If the condition within the 'while' loop is not specified, the program will not execute. To
execute it, the condition within the 'while' loop is must.
Proof:
#include <stdio.h>
#include <conio.h>
{
int i=10;
void main()
while()
{
printf("Hello");
}
getch();
}
When the above program is executed, it will not execute. It shows 'Expression syntax error'.
Multiple-choice Questions
1. In case 'for' loop, statement is terminated by semicolon .
(a) The compiler will flag error message and program will not compiled
(b) Program will be executed; however, scope of the 'for' loop will be NULL
(c) The termination will not affect the performance of the 'for' loop
2. Which of the following loop statement uses two keywords?
(a) for (b) do-while (c) while
3. Though the condition is false, which of the following loop statement executes once ?
(a) for (b) do-while (c) while
4. Entry-controlled loop is .
(a) for (b) do-while (c) while
5. Exit-controlled loop is .
(a) for (b) do-while (c) while
6. Which of the following loop statement is terminated by semicolon?
(a) for (b) do-while (c) while
M06_ITL-ESL4791_02_SE_C06.indd 111 12/22/2012 5:02:21 PM
..................Content has been hidden....................

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