Whats the difference between switch and if/else statements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Whats the difference between switch and if/else statements?

10th Sep 2017, 1:31 PM
Derek Leiro
Derek Leiro - avatar
8 Answers
+ 15
Switch allows you to write conditions based on ONE value which evaluate equality only. On the other hand, if-else is more general for any purpose with possible combination of logical operators (e.g. AND / OR).
10th Sep 2017, 3:16 PM
Zephyr Koo
Zephyr Koo - avatar
+ 10
Swich works similar to multiple if else statements. Using switch is simpler and will also save your time.
10th Sep 2017, 1:34 PM
Lakshay
Lakshay - avatar
+ 4
in simple words switch is like selecting a candidate based on roll assigned to him by calling his roll no just once. on the other hand if else is like selecting candidate after candidate until his name comes by calling him
15th Oct 2018, 12:30 PM
Vidhya Sagar
Vidhya Sagar - avatar
+ 3
As has been mentioned, switch statements only allow you to test for equality - specifically, for whether the value of some one variable matches one of a finite set of states. Think gears in a car (as opposed to speed or RPMs), or solar system planets (as opposed to distances from the sun). Also, switch statements allow you to execute code from any branch defined below the matching one by omitting the 'break' keyword. So, for example, you can call ActArrogant when matching degree == PhD, then continue down into the degree == BS branch and also call the HaveDebt method there. If/else requires nesting or duplication to accomplish same.
11th Sep 2017, 5:25 AM
Ilya Zarembsky
Ilya Zarembsky - avatar
+ 1
if else :use an nested conditions and steps to get the output and if the inputs are related to each other and independent. switch if the input was in the same type and not independent and you have multiple outputs sorry about my english
10th Sep 2017, 1:50 PM
Moustafa Shahin
Moustafa Shahin - avatar
+ 1
Is it like that for every language? (the one value condition thing)
29th Nov 2018, 4:24 AM
Muhammad Abu Baker
Muhammad Abu Baker - avatar
+ 1
Switch Choose from multiple choices and execute which fulfill conditions in ( ) If else work as booleon if true execute if false execute else
3rd Feb 2019, 12:32 AM
Rahul
Rahul - avatar
0
Switch allows us to move in our favorable condition whereas if allow us to enter only when condition is true
29th Nov 2018, 5:28 PM
Jkilop