Question

In: Computer Science

Question #14. a. TRUE OR FALSE: Code written using a case/switch construct can always be rewritten...

Question #14. a. TRUE OR FALSE: Code written using a case/switch construct can always be rewritten using an if/else construct. Explain why or why not. b. TRUE OR FALSE: Code written using an if/else construct can always be rewritten using a case/switch construct. Explain why or why not.

matlab

Solutions

Expert Solution

Question a:

Code written using a case/switch construct can always be rewritten using an if/esle construct.

Because in switch case, a variable value is checked with different cases which can also be achieved using if-elseif-else construct.

Eg:

switch(value){

case 1: //statements

case 2: //statements

case 3: //statements

default: //statements

}

The above switch statement code can be rewritten as

if(value==1)

{//statements}

else if(value==2)

{//statements}

else if(value==3)

{//statements}

else

{//statements}

Question b:

Code written using an if/else construct cannot always be rewritten using a case/switch construct.

It can be rewritten in the vice versa case of above explanation i.e.,condition checking is done on a single variable.

But in the case of conditions using multiple variables in different cases in if/else construct cannot be rewritten in switch/case construct.

Eg:

if(value==1)

{//statements}

else if(grade=='A')

{//statements}

else if(value==5)

{//statements}

else

{//statements}

The above if/else construct cannot be rewritten using switch/case construct.


Related Solutions

Indicate whether the following statements are always true or can be false. True False  If an object's...
Indicate whether the following statements are always true or can be false. True False  If an object's speed does not change, no net force is acting on the object. True False  If two objects are under the influence of equal forces, they have the same acceleration. True False  The net force which acts on an object which maintains a constant velocity is zero. True False  An object's velocity will change if a net force acts on the object. True False  A truck initially moving at...
1. Any “case” statement can be written as an “if” statement. True False 2.How many iterations...
1. Any “case” statement can be written as an “if” statement. True False 2.How many iterations does the following “loop” statement have? n = 1 loop do n = n + 1 puts “hey" next unless n = 10 break end 0 7 This loop is syntactically incorrect. 9 8 3.The following two conditional expressions are equivalent outcome wise: n = 1 begin puts n end while n < 1 n = 1 while n < 1 puts n end...
***Using Java Using the switch/case construct, write a program that takes a character from the user...
***Using Java Using the switch/case construct, write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’, …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same. If the user entered the character A,...
Indicate whether the following statements are always true or can be false. (Select T-True, F-False. If...
Indicate whether the following statements are always true or can be false. (Select T-True, F-False. If the first is F and the rest T, enter FTTTTT). A) In order not to slow down, a bicycle moving at a constant velocity needs a small net force applied. B) If two objects have the same acceleration, they are under the influence of equal forces. C) If a net force acts on an object, the object's velocity will change. D) During the collision...
Can this code be rewritten but keeping the same functions as the original and with detailed...
Can this code be rewritten but keeping the same functions as the original and with detailed comments. I'm going to post the original homework lab just in case. Attached to this assignment is a Java program that converts a text file to a list of hexadecimal numbers. Each of those hexidecimal numbers represents the bit pattern of a character from the file with the parity bits (even parity) for a hamming code inserted. Each text character takes 8 bits and...
Question:   Can you please convert this program into switch case program using c++. Output should be...
Question:   Can you please convert this program into switch case program using c++. Output should be same int main() {     bool weekend = false;     unsigned char day = 0;     bool isDay = true;     cout << " Enter a char for a day of week:";     cin >> day ;     if (day == 'M' || day == 'm')         cout << " This is Monday" << endl ;     else if (day == 'T' || day...
QUESTION 33 True or false? The sinoatrial node can be described as a pacemaker. True False
QUESTION 33 True or false? The sinoatrial node can be described as a pacemaker. True False
QUESTION 33 Average fixed cost always decreases. True False ------------------------ QUESTION 31 If a price ceiling...
QUESTION 33 Average fixed cost always decreases. True False ------------------------ QUESTION 31 If a price ceiling is imposed below the equilibrium price then _____. A. the market can still reach equilibrium B. excess supply is created C. demand will decrease D. supply will increase E. excess demand is created --------------- QUESTION 29 A price floor on corn would have the effect of which of the following? A. Creating an excess supply regardless of the price. B. Creating an excess supply...
True or false. 8._______ Agency rules can always be passed with no public comment period if...
True or false. 8._______ Agency rules can always be passed with no public comment period if the agency so desires. 9.______ An battery is an intentional touching without the other party's consent. 10. Committing a tort can expose the liable party to prison time.
Q: (TRUE) OR (FALSE): A foundation built on clayey soils should always be designed using total...
Q: (TRUE) OR (FALSE): A foundation built on clayey soils should always be designed using total stress parameters. ( ) Differential settlement causes less structural damage than total settlement.( ) The original bearing capacity equation was derived for plane strain conditions.( ) Shearing of overconsolidated clays causes negative excess pore water pressure development.( ) For normally consolidated clays, the angle of friction in terms of effective stresses is smaller than the angle of friction in terms of total stresses.( )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT