PROGRAMMING IN ANSI C
Chapter 5 BP=143
DECISION MAKING AND BRANCHING
REVIEW QUESTION:
RQ-5.1:State whether the following are true or false :
(a)When if statements are nested , the last else gets associated with the nearest
if without an else.
Ans: False.
(b)One if can have more than one else clause.
Ans: False.
(c)A switch statement can always be replaced by a series of if..else statements.
Ans: False.
(d)A switch expression can be of any type.
Ans: False.
(e)A program stops its execution when a break statement is encountered.
Ans: False.
(f)Each expression in the else if must test the same variable.
Ans: True.
(g)Any expression can be used for the if expression.
Ans: True.
(h)Each case label can have only one statement.
Ans: True.
(i)The default case is required in the switch statement.
Ans: True.
(j)The predicate !( (x>=10) (y==5) ) is equivalent to (x<10) && (y!=5 ).
Ans: True.
RQ-5.2:Fill in the blanks in the following statements:
(a)The ……….operator is true only when both the operands are true.
Ans: logical AND (&&).
(b)Multiway section can be accomplished using an else if statement or the . ……………statement.
Ans: switch.
(c)The……….. statement when executed in a switch statement causes.
immediate exit from the structure
Ans: break.
(d)The ternary conditional expression using the operator ?: code be easily
coded using ………..statement.
Ans: if…else.
(e)The expression !(x!=y)can be replaced by the expression……… Ans: x==y.
RQ-5.3:Find errors, if any, in each of the following segments:
Solution:
(a)if((x+y=z) && (y>0) )
printf(" ");
Ans: Error.
Correct ans: if((x+y==z) && (y>0) )
printf(" ");
(b) if (code >1)
a= b+c
else
a=0
Ans: Error.
Correct ans: if (code >1)
a= b+c;
else
a=0;
(c) if(p>0) || (q <0)
printf("Sign is negative”);
Ans: Error.
Correct ans:if((p>0) || (q <0))
printf("Sign is negative”);
RQ-5.4:The following is a segment of a program:
x=1;
y=1;
if(n>0)
x=x+1;
y=y-1;
printf("%d %d", x,y);
what will be the values of x and y if n assumes a value of (a) 1and (b) 0.
Solution:
(a)The value of x is 2 & y is 0.
(b)The value of x & y is imaginary.
RQ-5.5:Rewrite each of the following without using compound relations:
(a) if(grade<=59&&grade>=50)
second=second+1;
Solution:
if(grade<=59)
second=second+1;
if(grade>=50)
second=second+1;
(b) if ( number>100||number<0)
printf(“Out of range”);
else
sum=sum+number;
Solution:
if ( number>100)
printf(“Out of range”);
else if(number<0)
printf(“Out of range”);
else
sum=sum+number;
(c) if (M1>60&&M2>60||T>200)
printf(“Admitted\n”);
else
printf (“Not admitted”);
Solution:
if (M1>60)
printf (“Admitted\n”);
if (M2>60)
printf (“Admitted\n”);
else if(T>200)
printf (“Admitted\n”);
else
printf (“Not admitted”);
RQ-5.6:Assuming x=10 ,state whether the following logical expressions are true or false:
(a)x==10 && x>10 && !x Ans:False.
(b)x==10 || x> 10 && !x Ans:True.
(c)x==10 && x>10 ||!x Ans:False.
(d)x==10 ||x>10 || !x Ans:True.
RQ-5.7:Find errors,if any, in the following switch related statements.Assume that the variables x and y are of int type and x=1 and y=2.
Solution:
(a)switch(y);
Ans: Error.
Correct ans: switch(y)
(b)case 10;
Ans: Error.
Correct ans: case 10:
(c)switch(x+y)
Ans:No error.
(d)switch(x) {Case 2: y= x+y; break};
Ans: Error.
Correct ans: switch(x) {Case 2: y= x+y; break;}
RQ-5.8:Simplify the following compound logical expressions:
(a) !(x<=10) (b)!(x==10)||!((y==5)||(z<0))
Ans:(x>10) Ans: (x>0)
(c)!((x+y==z)&&!(z>5)) (d)!((x<=5)&&(y==10)&&(z<5))
Ans: (x<z) Ans: (x>5)
RQ-5.9:Assuming that x=5, y=0,and z=1 initially ,what will be their values after executing the following code segments?
(a)if(x && y)
x=10;
else
y=10;
Output:
10
10
(b)if(x|| y ||z)
y=10;
else
z=0;
Output:
1
0
(c)if(x)
if(y)
z=10;
else
z=0;
Output:
10
0
(d)if(x ==0 || x && y)
if(!y)
z=0;
else
y=1;
Output:
0
1
RQ-5.10:Assuming that x=2,y=1 and z=0 initially ,what will be their values after executing the following code segments?
(a)
switch(x)
{
case 2:
x=1;
y=x+1;
case 1:
x=0;
break;
default:
x=1;
y=0;
}
Output:
1
0
(b)
switch(y)
{
case 0:
x=0;
y=0;
case 2:
x=2;
z=2;
default:
x=1;
y=2;
}
Output:
0 0 0
RQ-5.11:Find the error ,if any,in the following statements:
Solution:
(a)if(x>=10)
printf("\n");
Ans: No error.
(b)if(x>=10)
printf("OK");
Ans: No error.
(c)if(x==10)
printf ("Good");
Ans : No error.
(d)if(x=<10)
printf("Welcome");
Ans : Error.
Correct ans: if(x<=10)
Printf(“Welcome”);
RQ-5.12:What is the output of the following program?
Program:
main()
{
int m=5;
if(m<3) printf("%d", m+1);
else if (m<5) printf("%d", m+2);
else if (m<7) printf("%d", m+3);
else printf("%d", m+4);
getch();
}
Output:
8
RQ-5.13:What is the output of the following program?
Program:
main ()
{
int m=1;
if( m==1)
{
printf ("Delhi");
if(m==2)
printf("Chennai");
else
printf("Banglore");
}
else
Printf("END");
getch();
}
Output:
1
Delhi
2
Chennai
3
Banglore
RQ-5.14:What is the output of the following program?
Program:
main()
{
int m;
for(m=1; m<5; m++)
printf("%d\n",(m%2) ? m : m*2);
getch();
}
Output:
1 4 3 8
RQ-5.15:What is the output of following program?
Program:
main()
{
int m,n,p;
for(m=0; m<3;m++)
for(n=0;n<3;n++)
for(p=0;p<3;p++)
if(m+n+p==2)
goto print;
print:
printf("%d %d %d",m,n,p);
getch();
}
Output:
0 0 2
RQ-5.16:What will be the value of x when the following segment is executed?
int x=10,y=15;
x= (x<y)? (y+x) : (y-x) ;
Solution:
The value of x after execution is :-25.
RQ-5.17:What will be the output when the following segment is executed?
int x=0;
if(x>=0)
if(x>0)
printf("Number is positive");
else
printf("Number is negative");
Output:
0
Number is positive
1
Number is negative
RQ-5.18: What will be the output when the following segment is executed?
Program:
char ch = ‘a’
switch(ch)
{
case ‘a’:
printf(“A”);
case ‘b’:
printf(“B”);
case ‘c’:
printf(“C”);
}
Output:
a
A
b
B
c
C
RQ-5.19:What will be the output of the following segment when executed?
Program:
main()
{
int x=10,y=20;
if(( x<y)|| (x+5)>10)
printf("%d",x);
else
printf("%d",y);
getch();
}
Output:
10
RQ-5.20:What will be the output of the following segment when executed?
Program:
main()
{
int a=10, b=5;
if(a>b)
{
if(b>5)
printf("%d",b);
}
else
printf("%d",a);
getch();
}
Output:
10
$ Any Problem $