2

I am currently studying the conditional constructions. Correct me if I am wrong but else if and else(if(){}) is the same thing... Example:

a=5;
if(a==6)
{ 
   Console.WriteLine("Variable 'a' is 6");
}
else if(a==5)
{
     Console.WriteLine("Variable 'a' is 5");
}

And

a=5;
if(a==6)
{ 
    Console.WriteLine("Variable 'a' is 6");
}
else
{
    if(a==5)
    {
        Console.WriteLine("Variable 'a' is 5");
    }
}

Are these things the same? And if yes why does else if exist if I can write it the "second way"(the second example that I wrote)?

7
  • No, they're not the same. They have the same effect in your example, but what if you had a third else if condition? Commented Feb 6, 2017 at 21:51
  • @TZHX I would have to write another else{if(){}} ? Or i can't have 2 elses can I? Commented Feb 6, 2017 at 21:52
  • @peter: And what would that look like? And what would it look like if you added even more conditions? Commented Feb 6, 2017 at 21:53
  • And then another if inside it, and another else containing another if. Can you see how many more curly brackets that would lead to? Commented Feb 6, 2017 at 21:53
  • 2
    Not just for the visual experience, but because it more clearly demonstrates what you want to achieve. Commented Feb 6, 2017 at 21:56

4 Answers 4

5

Yes, these are effectively identical.

The reason the "else if" statement exists is to make cleaner code when there are many conditions to test for. For example:

if (a==b) {
   //blah
} else if (a==c) {
   //blah
} else if (a==d) {
   //blah
} else if (a==e) {
   //blah
}

is much cleaner than the nested approach

if (a==b) { 
    //blah
} else {    
    if (a==c) {
        //blah
    } else {
        if (a==d) {
            //blah
        } else {
            if (a==e) {
                //blah
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Though lets be honest, cases like that are what switch is for.
You can't always use switch case-- like in my example, c, d, and e are variables, not constants, so you can't use them in case statements.
@Tim ok but can't i add all those ifs in only one else construnction? so it would look something like this if (a==b) { //blah } else { if (a==c) { //blah } if (a==d) { //blah } if (a==e) { //blah } }
@peter It depends on the logic you want to implement. Your suggestion is totally different from the example in this answer
@peter What happens if c==d? then you end up running both id a==c and if a==d with only the one else, and if you want to do that, then you are just testing the variable each time and there is no need for an else.
|
3

why does else if exist

It doesn't. It's not a keyword or construct on its own. Your two examples are identical except that in the second case you've added some superfluous braces and whitespace into the code.

if and else are both simply followed by a single statement. In your first example the statement following the else is:

 if(a==5)
{
     Console.WriteLine("Variable 'a' is 5");
}

The second example just wraps that same statement in braces, and adds a new line at the start. The new line is ignored, so it doesn't change the semantics, and as the code is already a single statement, wrapping it in braces doesn't change it in any way.

1 Comment

ohhhhhhhhhhhhh now i get it ... So basically "else if" is just using advantage that you don't need to use braces if it's a single line? And that why else { if(){}} == else if ?
2

Strictly speaking, there is no such thing as an else if statement. An "else if" is actually just in essence an else with a single line body that happens to be the start of an entirely separate if statement. You can visualize it like this:

var a = 5;

// This if uses a single line
if (a == 6) DoSomething();
// This else is a single line that is also a single-line if
else if (a == 4) DoAnotherThing();
// This else uses a single line as well, but is referring instead to the second if
else DoSomethingElse();

The above script is identical to the following:

if (a == 6) 
{
    DoSomething();
}
else 
{
    if (a == 4) 
    {
        DoAnotherThing();
    }
    else 
    {
        DoSomethingElse();
    }
}

Or even this:

if (a == 6) 
    DoSomething();
else 
    if (a == 4) 
        DoAnotherThing();
    else 
        DoSomethingElse();

The reason that it is written as else if so commonly is because it compliments the logical flow of the code. That, and it just looks so much prettier.

1 Comment

The last statement should be reworded to "DoSomethingCompletelyDifferent" - just kidding :o)
0

Mostly it makes the code cleaner, easier to read, makes indenting better, particularly if you have any conventions on character length of rows (and if you don't then you've got thirty indents in a big if statement, such a pain to read). It also saves space, a few extra characters and indents across thousands of lines may not be much, but why use it if you don't have to. When the code compiles they will pretty much be the exact same in the DLLs anyways.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.