+ 1

What is the difference between else and else if statement

29th Oct 2017, 8:27 PM
Mena Hany
Mena Hany - avatar
2 odpowiedzi
+ 4
You could even have only two options and use an extra condition ^^ if // first condition else if // second condition You have just to be sure that those conditions are enough for your needs, as if both conditions are false, none of the if blocks will be executed ;) if (x < 0) { // do something } else { // do another thing for all other cases (implicitly if not x<0, so if x>=0 } if (x < 0) { // do something } else if (x <42) { // do another thing if x<42, but also implicitly if x>=0 (because if x<0 the first if block will be executed, and then other conditions will be jumped without testing them... } // and so, implicitly, if x>=42, nothing have occured and code execution continue from here
29th Oct 2017, 8:42 PM
visph
visph - avatar
+ 1
If you have 2 options you use " if" and " else". if you have 3 options you can add an extra comparison. if //first comparison else if //second comparison else //default if non above applied
29th Oct 2017, 8:30 PM
sneeze
sneeze - avatar