+ 1
Where the switch statement is useful?
I have no idea why the switch statement would be used.
4 Respostas
+ 3
sometimes it looks more clear than an if statement with the same logic, but thats subjective
I also heard that the compiler may optimize the switch statement if there are many cases inside it to spend less time checking each individual case, as you would with if statement
more practical use would be this: you want action A performend on condition Q, actions A and B performed on condition W, actions A and B and C performed on condition E. You do this:
.....
case E:
C;
case W:
B;
case Q:
A;
notice the absence of "break;" before next case, it allows the program to execute cases ahead of the matching one
+ 3
Seb TheS consider the case of date format...
input is 12-03-2018 and you are suppose to print 12-mar-2018..
for this scenario, you need 12 case for each month.... here, switch case is effective compared to if and else if statement
0
Data well I expected that, but I thought it could be performed with for loops as well, switch statement increases readability, but I still have no idea where it would be used.
0
Data
Arrays seem more powerful