+ 5
Whats the difference between switch and if/else statements?
8 Antworten
+ 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).
+ 10
Swich works similar to multiple if else statements.
Using switch is simpler and will also save your time.
+ 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
+ 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.
+ 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
+ 1
Is it like that for every language?
(the one value condition thing)
+ 1
Switch Choose from multiple choices and execute which fulfill conditions in ( )
If else work as booleon if true execute if
false execute else
0
Switch allows us to move in our favorable condition whereas if allow us to enter only when condition is true