I was wondering why you would use an else if
statement, and not multiple if
statements? For example, what's the difference between doing this:
if(i == 0) ... else if(i == 1) ... else if(i == 2) ...
And this:
if(i == 0) ... if(i == 1) ... if(i == 2) ...
They seem to do the exact same thing.
In general, "else if" style can be faster because in the series of ifs, every condition is checked one after the other; in an "else if" chain, once one condition is matched, the rest are bypassed.
There is really no difference between this and else if. The whole advantage is that nesting kills the readability after 2 or 3 levels so it's often better to use else if. So as the default case handles all possibilities the idea behind else if is to split this whole rest into smaller pieces.
if(i == 0) ... //if i = 0 this will work and skip the following else-if statements else if(i == 1) ...//if i not equal to 0 and if i = 1 this will work and skip the following else-if statement else if(i == 2) ...// if i not equal to 0 or 1 and if i = 2 the statement will execute if(i == 0) ...//if i = 0 this will work and check the following conditions also if(i == 1) ...//regardless of the i == 0 check, this if condition is checked if(i == 2) ...//regardless of the i == 0 and i == 1 check, this if condition is checked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With