Review question’s answer
Chapter
no-06(Decision making and looping)
===========================================
6.1 State whether the
following statements are true or false:
(a)
The do… while statement first executes the loop body and then evaluate the loop
control expression. Ans:
True.
(b)
In a preset loop, if the body is executed n terms, the test expression is
executed n+1 times. Ans:
True.
(c)
The number of times a control variable is updated always equals the number of
loop iterations. Ans:
True.
(d)
Both the preset loops include initialization within the statement.
Ans:
True.
(e)
In a for loop expression, the starting value of the control variable must be
less than its ending value. Ans:
True.
(f)
The initialization, test condition and increment parts may be missing in a for
statement. Ans:
False.
(g)
While loops can be used to replace for loops without any change in the body of
the loop. Ans: False.
(h)
An exit control loop is executed a minimum of a one line.
Ans:
False.
(i)
The use of continue statement considered as unstructured programming. Ans:
True.
(j)
The three loop expressions used in a for loop header must be separated by
commas. Ans:
True.
6.2: Fill in the
blanks in the following statements.
(a)
In an exit controlled loop, if body is executed n times, test condition is
evaluated times. Ans:
(n-1)
(b)
The statements is use to skip a part of the statements in a loop.
Ans:
continue.
(c)
A for loop with the no test condition is known as loop.
Ans:
infinite
(d)
The sentinel controlled loop is also; known as loop.
Ans:
indefinite repetition.
(e)In
a counter controlled loop, variable known as is used to count the loop
operation. Ans:
definite repetition.
6.4: What is a null statement? Explain
a typical use of it.
Answer:
The
"null statement" is an
expression statement with the
expression missing. It is useful when the syntax of the language calls for a statement but no expression
evaluation. It consists of a semicolon.
The
null statement is useful with the if , while , do , and for statements. The most common use of
this statement is in loop operations in which all the loop activity is
performed by the test portion of the loop. For example, the following statement
finds the first element of an array that has a value of 0:
for (i=0; array[i] != 0;
i++)
6.5: Use of goto should be avoided.
Explain a typical example where we fint the application of goto becomes
necessary.
Answer:
Because most of the time, programs written
using goto statements can be
rewritten using conditionals and loops so that code is more readable. A large
fraction of the programming community (wrongly) interpreted it as a call to avoid goto statements altogether. This
is bad advice.
6.8 explain the
operation of each of the following for loops.
(a)for (n=1;n!=10;n+=2)
sum=sum+n;
Ans :The loop repeats 5 times.
|
(b)for(n=5;n<=m;n-=1)
sum+=n;
Ans: The continue until n<=m
where m initializes from 5 and decrements by 1.
|
(c) for(n=1;n<=5)
sum+=n;
Ans: Since theren is no increment
or decrement condition the loop repeats 5 times.
|
(d) for(n=1; ;n+=1)
sum+=n;
Ans: The loop repeats infinity
times.
|
(e)for(n=1;n<5;n++)
n=n-1;
Ans: The loop repeats infinity
times.
|
6.9: What would be
the output of each of the following code segments?
(a)count=5;
while(count-- >0)
printf(“count”);
|
(b)count=5;
while(-- count>0)
Printf(“count”);
|
(c) count=5;
do printrf(“count”);
while(count>0)
|
Output:
5 4 3 2 1
|
Output:
4
3 2 1
|
Output:
5
4 3 2 1
|
(d) for(m=10;m>7;m-=2)
printf(“m”);
|
(d)
output:
10 8
|
6.11:Analyse each of
the program segment that follow the determine how many times the body of each
loop will be executed.
(a) x=5;
y=50;
while(x<=y)
{ x=y/x;
…………………..
………………….. }
Ans: Infinity times
|
(b) m=1;
do
{ ……………………
……………………….
m+=2; }
while(m<10)
Ans: 5 times.
|
(c) int i;
for(i=0;i<=5;i=i+2/3)
{
…………………..
……………………. }
Ans: Infinity times.
|
(d) int m=10;
Int n=7;
while(m%n>=0)
{
………………
m+=1;
n+=2;
……………. }
Ans: 4 times.
|
6.12: Find errors, if
any, in each of the following looping segments. Assume that all the variables
have been declared and assigned values.
(a)while(count!=10);
{
count=1;
sum+=x;
count+=1;
}
Error: while(count!=10);
Correct Ans: while(count!=10)
|
(b) name=0;
do
{
name+=1;
printf(“my name is Dinar\n”);
while(name=1);
Error: while (name=1);
Correct Ans: while(name==1);
|
(c) do;
total+=value;
scanf(“%f”, &value);
while(value!=999);
Error: do;
Correct Ans: do
|
(e) m=1;
n=0;
for(p=10;p>0;)
p-=1;
printf(“%f”,p);
Error:
|
(f) for(p=10;p>0;)
p-=1;
printf(“%f”,p);
Correct ans: for(p=10;p>0;)
{
p-=1;
printf(“%f”,p); }
|
6.13:Write a for
statement to pront each of the following sequence of integers:
(a) 1,2,4,8,16,32
Ans: for(i=1;i<=32;i=i*2)
printf(“%d”,i);
|
(b) 1,3,9,27,81,243
Ans: for(i=1;i<=243;i=i*i)
printf(“%d”,i);
|
(c) -4,-2,0,4
for(i=-4;i<=4;i=i+2)
printf(“%d”,i);
|
(d)
-10,-12,-14,-18,-26,-42
for(i=-10;i<=-42;i=i-2)
printf(“%d”,i);
|
6.14: Change the
following for loops to while loops :
(a)for(m=1;m<10;m=m+1)
printf(“m”);
Ans: m=1;
while(m<10)
{
…………….
m++;
} printf(“m”);
|
(b)for(;scanf(“%d”,&m)!=-1;)
printf(“m”);
Ans:
while(scanf(“%d”,&m)!=-1)
printf(“m”);
|
6.16: What is the
output of following code?
Int m=100,n=0;
while(n==0)
{
if(m<10)
break;
m=m-10; }
|
Output: No output
|
6.17: What is output
of the following code?
int m=0;
do
{
if(m>10)
continue;
m=m+10;
}
while(m<50);
printf(“%d”,m);
|
Output: 50
|
6.18: What is the
output of the following code?
int n=0,m=1;
do
{
printf(“m”);
m++; }
while(m<=n);
|
Output: 1
|
6.19: What is the
output of the following code?
int n=0,m;
for(m=1;m<=n+1;m++)
printrf(“m”);
|
Output: 1
|
6.20: When do we use
the following statement?
for(; ;)
Answer:
When we need an infinity loop the
statement for(; ;) can be used.
-----------------------------------------------------------------------------------------------------------------------------
- You can download this file as pdf by clicking below download icon-
- Download
Go ahead by honesty--->Rm
No comments:
Post a Comment