home / study / engineering / computer science / computer science questions and a
ID: 3737698 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / 1) can a switch statement do the same thing as an if else statement? if so, give an example. ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: 1) Can a switch statement do the same thing as an if else statement? if so, give an example. 2) W...
1) Can a switch statement do the same thing as an if else statement? if so, give an example.
3) What are the uses for the switch statement ?
4) Why does the switch start in the OFF state ?
5) Is it necessary to have switch statement, or can you do basically the same with if, and if else statements ?
6) Can you have fall through with cascading if statements ?
Explanation / Answer
1-
Yes swich statements and if else statements can do same thing, as in;
swich(ch)
{
case 1:
//task 1
break;
case 2:
//task 2
break;
case 3:
//task 3
break;
default:
//default task
break;
}
if(ch == 1)
//task 1
else if(ch == 2)
//task 2
else if(ch == 3)
//task 3
else
//default task
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3) What are the uses for the switch statement ?
the main and basic use of switch statements is to show the branching and controlling each branch with a sequence of instructions.
here, bramching refers to the more than one choices.
if any expression can evaluate in different values, and thew flow of execution depends on this value, the for each case, switch is used.
switch statements are used where there are multiple choices.(like in menu driven programs)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The switch start in OFF state, because initially, the condition is not defined, and if without condition we execute a switch statement, it should not go to any branch.
That is why, initially it is False or OFF
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5) Is it necessary to have switch statement, or can you do basically the same with if, and if else statements ?
No, it is not necessary to have a switch statement, the same thing can be performed by multiple if-else statements.
in multiple if-else statements we need to define the condition each time, but in switch we just need to define the control condition once.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Can you have fall through with cascading if statements ?
Yes, Fall Through can be achieved from cascading if statements.
for example,
if(ch == 1)
//task 1
if(ch == 2)
//task 2
if(ch == 3)
//task 3
if(ch == 4)
//task 4
These if statements perform the same task as-
switch(ch)
{
case 1:
//task 1
case 2:
//task 2
case 3:
//task 3
case 4:
//task 4
}
If there is anything that you do not understand, or need more help with then please mention it in the comments section.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.