Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Difference between if and if else and if else if?
Posted
Comments
OriginalGriff 9-Nov-13 9:22am    
Don't post a new question to improve your existing one: Use the "Improve question" widget to edit your question and provide better information instead.
I have deleted the older version.
Thomas Daniels 9-Nov-13 9:23am    
And with that, you also deleted my answer!
But that's no problem, I was able to find it back.
OriginalGriff 9-Nov-13 9:44am    
There was no answer when I deleted it! :laugh: (otherwise, I would have deleted this one instead...)
Sorry about that - it's annoying when that happens, isn't it?
Thomas Daniels 9-Nov-13 9:51am    
Probably I posted my answer after you deleted the question. Anyway, it's not a problem, because I was able to find back my answer using the ListVersions.aspx page, and I posted it again here.
BillWoodruff 9-Nov-13 10:03am    
Whoa, ProgramFox, what's this ListVersions.aspx ? thanks, Bill

C#
if (5 == 5)
{
// some code
}

In this case, if 5 is equal to 5, then the code inside the ¬
if block is executed.
C#
if (5 == 4)
{
// some code
}
else
{
// some other code
}

In this case, if 5 is equal to 4, then the code inside the if block is executed.
If 5 is NOT equal to 4, then the code inside the else block is executed.
C#
if (5 == 4)
{
// some code
}
else if (5 == 6)
{
// some other code
}

In this case, if 5 is equal to 4, then the code inside the
if block is executed.
If 5 is not equal to 4, then the code in the else if block will only get executed if 5 is equal to 6.

Hope this helps.
 
Share this answer
 
v2
There isn't one: else if in c# is just an elseclause that contains an if statement:
C#
if (a)
   {
   }
else if (b)
   {
   }
else
   {
   }
Is actually the same as:
C#
if (a)
   {
   }
else
   {
   if (b)
      {
      }
   else
      {
      }
   }
But the first version is easier to read.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900