Problem 3 Short answer questions 1 Assume the variables x 2
Problem 3. Short answer questions.
(1) Assume the variables x = 2 and y = 4. Is the following expression true or false?
(x==4)||(y!2)
(2) What is wrong with the following switch statement?
switch(temp) { case temp<0: cout<< “Temp is negative.\ ”;
break;
case temp==0: cout<< “Temp is zero.\ ”;
break;
case temp>0: cout<< “Temp is positive.\ ”;
break;
}
Solution
(1)
Given x=2 and y=4
(x==2)||(y!2) is false statement
since the y!2 is incorrect test statemnet
The correct statement is
(x==2)||(y!=2)
where != to check y is not equal to 2
(2)
The switch statement takes input argument
and selectes the matched case label from several
alternatives and executes the corresponding statements.
switch(temp)
{
//Conditions cannot be tested in case label
//temp<0 is incorrect label
case temp<0:
cout<< “Temp is negative.\ ”;
break;
//Conditions cannot be tested in case label
//temp==0 is incorrect
case temp==0:
cout<< “Temp is zero.\ ”;
break;
//Conditions cannot be tested in case label
//temp>0 is incorrect
case temp>0:
cout<< “Temp is positive.\ ”;
break;
}
