Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between If and Else If?

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.

like image 936
sparklyllama Avatar asked Nov 28 '13 06:11

sparklyllama


People also ask

Which is better if or else if?

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.

What is the difference between if else and ELSE IF in C programming?

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.


1 Answers

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 
like image 75
Damodaran Avatar answered Sep 22 '22 00:09

Damodaran