Question

In: Computer Science

In what situation would you prefer to implement several alternative logical branches using Multi-Way if/else statements?...

In what situation would you prefer to implement several alternative logical branches using Multi-Way if/else statements? When would you prefer to use Switch?

Solutions

Expert Solution

Greetings!!!!!

Please find my answer as below.

Firstly the major difference between Multi-Way if/else statements and Switch statements are as below.

Multi-way if else Statement

Switch Statement

Testing Condition

Multi-way if-else Statement can be used to test the condition on the range of values.

Switch statement can be used to test the condition on single integer, enumerated value, character, and string object.

Speed of Execution

If selection is there among large group of values then Multi-way if else is slower.

If selection is there among large group of values then switch is faster.

Suitability

Multi-way if-else Statement is better for checking range of values.

Switch statement is better for checking fixed values.

Number of expressions

Multi-way if-else Statement has multiple numbers of expressions to be evaluated.

Switch statement has a single expression to be evaluated.

Decision making

In case Multi-way if-else statement, decisions are made on Boolean result of each expression.

In case of switch statement, decisions are made on equality of fixed value with expression value.

Entering in blocks

Multi-way if-else Statement checks for true condition and executes only corresponding block of statements for found true condition. Once executed control comes out of the entire statement.

Switch statement checks for the first matching and executes statements corresponding to that block and the blocks further below to it until an explicit statement is used to exit entire statement.

Now the typical syntax of both statements are as below.

Multi-Way if/else statements:---

if( condition-1)

   block of statement(s)-1;

else if (condition-2)

    block of statement(s)-2;

else if (condition-3)

    block of statement(s)-3;

else if (condition-4)

block of statement(s)-4;

else if (condition-n)

    block of statement(s)-n;

else

    default block of statement(s);

Switch statements:----

switch(expression)

{

    case constant-1

        block of statement(s)-1;

        break;

    case constant-2

        block of statement(s)-2;

        break;

    case constant-3

        block of statement(s)-3;

        break;

    case constant-n

        block of statement(s)-n;

        break;

    default:

        default_block of statement(s)-;

}

When to use Multi-Way if/else statement----

1) When you need to check range of values in the form of Boolean expression.

2) When number of cases to be checked are less in number.

3) When only one block of statements to be executed.

When to use switch statement-----

1) When you need to check equality of particular expression value.

2) When number of cases to be checked are large in number.

3) When you need to execute multiple of statements after a match is found.

Thank You


Related Solutions

All else being equal, would you prefer to work in a tall or flat organization? Why?...
All else being equal, would you prefer to work in a tall or flat organization? Why? Which one do you think is best: a narrow span of control, or a wide span of control? How does the concept of span of control relate to tall/flat organizations? Justify your reasoning.
All else being equal, would you prefer to work in a tall or flat organization? Why?...
All else being equal, would you prefer to work in a tall or flat organization? Why? Which one do you think is best: a narrow span of control, or a wide span of control? How does the concept of span of control relate to tall/flat organizations? Justify your reasoning. 300 words
If you had a dispute, would you prefer to litigate or use alternative dispute resolution? Why?...
If you had a dispute, would you prefer to litigate or use alternative dispute resolution? Why? If you had to use alternative dispute resolution, which form would you choose? Discuss the benefits of the method using a specific example.
Which way would you prefer your company share their wealth? Why?
Which way would you prefer your company share their wealth? Why?
What are the advantages and disadvantages of decentralization? Would you prefer to work in a centralized...
What are the advantages and disadvantages of decentralization? Would you prefer to work in a centralized or decentralized organization and why? What are opportunity costs and what are the economic benefits? Can you think of various industries that these types of costs and benefits could be applied?
1. what financial statements would be utilized in this proposal: to implement a sliding fee scale...
1. what financial statements would be utilized in this proposal: to implement a sliding fee scale for patients who require financial assistance. 2. and how will those financial statements be used
Consider a real-life business situation and discuss under what circumstances you would consider using the MERGE...
Consider a real-life business situation and discuss under what circumstances you would consider using the MERGE statement. In addition to detail explanation, provide an SQL example to merge at least three tables. Your example should be unique (do not use textbook and/or internet examples).
How would you handle this situation? What would you advise Orlando to do in this meeting?...
How would you handle this situation? What would you advise Orlando to do in this meeting? What would you tell his manager, if anything? Are there any systemic changes you could think of that may help prevent instances like these from happening in the future? The annual reviews have just been completed, and you heard from Orlando Nicholson, an employee who had been with the firm for two years. Orlando has a cordial but distant relationship with his manager. He...
Microscope Question: What type of microscopy would you use for each situation situation and why and...
Microscope Question: What type of microscopy would you use for each situation situation and why and how does it work Routine examination of cheek cells in a classroom setting Tracking the localization of a transgenic protein from DNA transformed into the cell. Examining and generating 3D images of very small ultrastructures of cells Examining a specimen that is visible to an ordinary light microscope but in a specimen that is transparent without staining You wish to internally observe a complex...
In what situation would you expect to order of a hypotonic solution application?
In what situation would you expect to order of a hypotonic solution application?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT