Why IF ELSE statement is preferred over,SWITCH CASE statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why IF ELSE statement is preferred over,SWITCH CASE statement

28th Mar 2017, 5:01 PM
Arpit Gupta
Arpit Gupta - avatar
9 Answers
+ 16
It is only preferred if it provides better readability. And if you use more than 3 nested if - it does ^^
28th Mar 2017, 5:04 PM
Tashi N
Tashi N - avatar
+ 14
@chirag String is possible in switch with Java8.
28th Mar 2017, 6:07 PM
Tashi N
Tashi N - avatar
+ 12
@Arpit I don't understand your answer.
28th Mar 2017, 5:08 PM
Tashi N
Tashi N - avatar
+ 11
Published a code example: https://code.sololearn.com/cQBKg9QBBy1R/?ref=app Check comments at the very top of the code.
28th Mar 2017, 7:09 PM
Tashi N
Tashi N - avatar
+ 9
if-else can be used in many places where u cannot use switch case. like,if u want to compare string,floating point numbers etc. u cannot use switch case. also switch case can only compare if the value is equal not < or > . so if-else is preferred.
28th Mar 2017, 5:16 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 8
@tashi N oh thanks!! i didnt know about java8!!
28th Mar 2017, 11:50 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 6
If else can use difficult expressions.Example, in switchcase , case x+8 is not possibe, whereas its possible in if else.
28th Mar 2017, 5:57 PM
Meharban Singh
Meharban Singh - avatar
0
what's is after does
28th Mar 2017, 5:05 PM
Arpit Gupta
Arpit Gupta - avatar
0
Hi, I am new to JAVA, and didn't know about the switch case limitations. I tend to write for readability since performance is rarely an issue with the fast computer speeds these days. Can anyone provide examples of IF statements vs. CASE statements? It would be very helpful...
28th Mar 2017, 5:50 PM
James Thomas
James Thomas - avatar