Where the switch statement is useful? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Where the switch statement is useful?

I have no idea why the switch statement would be used.

29th Aug 2018, 9:35 AM
Seb TheS
Seb TheS - avatar
4 Answers
+ 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
29th Aug 2018, 9:57 AM
Data
Data - avatar
+ 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
29th Aug 2018, 11:15 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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.
29th Aug 2018, 10:22 AM
Seb TheS
Seb TheS - avatar
0
Data Arrays seem more powerful
29th Aug 2018, 11:19 AM
Seb TheS
Seb TheS - avatar