abdullah cinar
abdullah cinar

Reputation: 543

Which order does the else statement follow?

I wanted to be sure about something,

When i have multiple if/else conditions and if does not imply an else condition for one of those if conditions; does the next else statement goes for the last if statement?

For example:

if(condition1)
{
    //operation1
}

if(condition2)
{
    //operation2
}

else
{
    //operation3.  
}

Like above example, if i don't use an else for the first if statement, which if statement does this else work for? Would this cause a problem for me, if i don't specify the else for each if?

I made some tests but wanted to be sure about how does this works actually.

Upvotes: 0

Views: 718

Answers (12)

user6023251
user6023251

Reputation:

always else is related to its preceding if statement.

Upvotes: 2

Leo The Four
Leo The Four

Reputation: 699

There are three different types of selection statements.

Single Selection Statement (if)

  • It performs an action only if the condition is true

Double Selection Statement (if...else)

  • It performs an action if the condition is true and another action if the condition is false

Multiple Selection Statement (Switch)

  • Performs different actions based on the possible values of constant internal expression

In your case the first if is single selections statement and when it reaches the end of block } it will continue the sequential processing of the following statements in the program.

Since your else is right after your second if therefore it makes this statement a double selection statement. Which perform the action in if block in case the result is true and the action in else in case the result is false.

Upvotes: 1

user4783404
user4783404

Reputation:

I don't know what you want to do exactly ? but you could use the if /else if statement of you could use a swtich.

example if-else:

      if(condition 1)
      {
        // your code here
      }
      else if(conditition2)
      {
         //your code here
      } 
      else
      {
        // your code here
       } 

or a swtich:
var caseSwitchLastName = "De Waard"
var caseSwitchFirstName = "Pieter";
switch (caseSwitchFirstName)
{
    case Pieter:
       Console.WriteLine("His name is Pieter");
       break;
    case Sander:
       Console.WriteLine("His name is Sander");
       break;
    default:
       Console.WriteLine("His name is de Waard");
       break;
} 

both codes have the same output. Only the examples are less typing.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

Quoting the C11 standard, chapter §6.8.4.1, The if statement (emphasis mine)

An else is associated with the lexically nearest preceding if that is allowed by the syntax.

So, your else statement is bound to lexically nearest preceding if statement, which is the if(condition2) statement.

Upvotes: 2

STF
STF

Reputation: 1513

The else is for the last if that is before the else.

you can see more here: http://www.cplusplus.com/doc/tutorial/control

Good luck!

Upvotes: 0

haccks
haccks

Reputation: 106032

if i don't use an else for the first if statement, which if statement does this else work for?

Second if statement.
The rule is that else will go with the nearest if in case of multiple ifs.

C11 §6.8.4.1/3:

An else is associated with the lexically nearest preceding if that is allowed by the syntax.

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

In C (and other C-like programming languages), you can place additional accolades to make statements more clear. Your code is equivalent to:

{
    if(condition1) {
        //operation1
    }
}

{
    if(condition2) {
        //operation2
    } else {
        //operation3.  
    }
}

The else statement is bounded to the second if statement. It means that the program will first evaluate condition1. If condition1 holds, it will execute operation1. Next regardless of the result of the first test, it will test condition2. If condition2 holds, it will execute operation2 and otherwise it will execute operation3. The else always binds with the nearest if (bottom-up) that is not yet bounded that is not yet bounded by an else, or using curly braces as brackets for another binding policy.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

The 'else' is always attached to the nearest 'if'. In other words, the last else is paired with the last if.

It is best for readability to use curly braces and proper indentation.

Upvotes: 2

4pie0
4pie0

Reputation: 29724

The closest. An else is associated with the lexically nearest preceding if that is allowed by the syntax.

if(condition1)
{
//operation1
}

if(condition2)
{
//operation2
} else {          // <<<<
//operation3.  
}

Upvotes: 2

pmg
pmg

Reputation: 108938

From the Standard, p 6.8.4.1

An else is associated with the lexically nearest preceding if that is allowed by the syntax.

So, in your example, the else pertains to the 2nd if (the one with condition2).

Upvotes: 12

Hatted Rooster
Hatted Rooster

Reputation: 36483

An else statement must always directly be followed after an if statement (or it's block). So your else in the example would affect if(condition2).

Upvotes: 2

Wind Mee
Wind Mee

Reputation: 25

The else statement is bound to the last if statement. The first if statement doesn't have an else due to the second if statement

Upvotes: 2

Related Questions