Help me with this Switch please(por favor) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help me with this Switch please(por favor)

https://code.sololearn.com/cJDhS9Jei1X7/?ref=app

5th May 2023, 8:31 PM
david guzman
david guzman - avatar
6 Answers
+ 1
You shouldn't use conditionals for switch cases, use if/else if/else chains instead. You outright can't use conditionals for switch in the sololearn playground, but even in environments where it's permitted, it's bad practice. For educational value though, some environments will let you do this if you use switch(true), because switch X case Y checks if X==Y
5th May 2023, 9:09 PM
Orin Cook
Orin Cook - avatar
+ 1
People, thank you very much for your attention. They motivate me to continue studying. So "switch" is usually not a good practice.
6th May 2023, 12:07 PM
david guzman
david guzman - avatar
0
Well, there's definitely something to be said for that too lol, but the main issue here is that your cases need to be definite values, preferably constants (eg string literals instead of a named variable, or commonly the values from an enum)
6th May 2023, 12:40 PM
Orin Cook
Orin Cook - avatar
0
CyberFox friend.. In this exemplo I can apply *switch *.. Then I don't understand why not can aplly *Switch * in the first exercise... You can or anyone can help me? https://code.sololearn.com/c7LFxOHQpOED/?ref=app
9th May 2023, 11:01 AM
david guzman
david guzman - avatar
0
Note the case statements: case 1, case 2. 1 and 2 are definitely constants lol. Switch cases need to be simple values likes this.
9th May 2023, 3:40 PM
Orin Cook
Orin Cook - avatar
0
I understand, that means that the cases of the Switch cannot be more complicated than an int, double or spring value. Correct?
13th May 2023, 2:22 AM
david guzman
david guzman - avatar