0

Help understanding switch please

Didn't understand the logic of this.

11th Feb 2017, 6:14 PM
Emil Pesaxov
Emil Pesaxov - avatar
2 Réponses
+ 6
The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.
11th Feb 2017, 6:50 PM
Cory Gutierrez🔹
Cory Gutierrez🔹 - avatar
0
If else if else if else.... Switch creates cases to match. Reads easily, makes more sense, less code. If you have to test if someone is younger than 18 would it not make more sense to check if person <18 than if person 10, else if person 11 etc. Switch tests multiple conditions until it finds true or defaults/breaks, basically to answer you in one sentence, use switch when you have more than 3-4 conditions to test otherwise you end up with nested, long confusing if else tests.
11th Feb 2017, 7:44 PM
Andre van Rensburg
Andre van Rensburg - avatar