Difference between if else statement and case | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Difference between if else statement and case

25th Feb 2017, 6:01 AM
adarsh singh
2 Answers
0
The "if-else" clause can only handle for 2 different case conditions that you would like to evaluate. "switch-case" clause, however is the same as "if-else if- else" clause and can handle as many conditions as you specified for each case.
25th Feb 2017, 6:11 AM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
0
In addition to Corsair Alex's answer, you can check for ranges inside an if-statement (e.g. if(x > 0 && x < 10)) whereas in the switch-case, you want to pick some discrete values out of a greater set. This is when switch-case really shines. e.g. switch(x){ case 2: <some code>; break; case 5: <some other code>; break; default: <completely other code>; }
25th Feb 2017, 7:34 AM
Huegel
Huegel - avatar