ANSWERED Which nested "if" gets the "else"

I think i should know this, but in the snippet below, to which "if" does the "else" belong to, the inner or outer; I mean it to belong to the inner one.

if (incomingIRCode == 123435)
{
   if (ledOn== false)
     {
        digitalWrite(ledPin, HIGH);
       ledOn=true;
     }
else
   {
       digitalWrite(ledPin, LOW);
      ledOn=false;
    }
}

Can't test right now, and I posted this as a response elsewhere, but I have a feeling the "else" will belong to the outer "if"?

To the inner.

Auto Format is your friend

if (incomingIRCode == 123435)
{
  if (ledOn== false)
  {
    digitalWrite(ledPin, HIGH);
    ledOn=true;
  }
  else
  {
    digitalWrite(ledPin, LOW);
    ledOn=false;
  }
}

Now you can see visually that the else aligns with the inner if.

It doesn't even get looked at if the outer condition is false.

Ok cool thanks....

So my follow-up..... what do I do if I want it like I have it, and then an else on the outer one as well? Ah wait, answering that myself.... the second block would be below the last } and therefore belong to the outside one?

if (incomingIRCode == 123435)
{
  if (ledOn== false)
  {
    digitalWrite(ledPin, HIGH);
    ledOn=true;
  }
  else
  {
    // so this belongs to inner if
    digitalWrite(ledPin, LOW);
    ledOn=false;
  }
}
else
{
  // this belongs to the outer one?
}

If there is doubt, use braces. Then neither you, nor anyone else, will be confused.

Ah wait, answering that myself.... the second block would be below the last } and therefore belong to the outside one?

If (this is an answer) { no comment } else /* next question */ {yes}. :slight_smile:

Or you could just keep it simple

 digitalWrite(ledPin,(incomingIRCode == 12345)? (ledon)? LOW:HIGH:somethingElse);

KenF:
Or you could just keep it simple

 digitalWrite(ledPin,(incomingIRCode == 12345)? (ledon)? LOW:HIGH:somethingElse);

I doubt if it's appropriate to introduce the beginner (ie, more of a beginner than I am) who asked the original question to a relatively advance coding technique like the ternary operator.

Anyhoo, my question is answered, thanks chaps.

KenF:
Or you could just keep it simple

 digitalWrite(ledPin,(incomingIRCode == 12345)? (ledon)? LOW:HIGH:somethingElse);

This is a new meaning of the word "simple" of which I was previously unaware. :wink:

:wink: indeed :slight_smile:

I can already see this turning int an IOCCC entry.

Shpaget:
I can already see this turning int an IOCCC entry.

I'd never heard of such a thing. Thanks for the heads up! Gives me something to browse through for the rest of the evening :slight_smile:

I find it handy sometimes to let the Arduino IDE find the closing brace. Just select the opening brace and Arduino will draw a box around the closing brace. Works with brackets in conditions and formulas too.

Interesting that this won't work if the selection is made from right to left.

dlloyd:
Interesting that this won't work if the selection is made from right to left.

It does

JimboZA:

dlloyd:
Interesting that this won't work if the selection is made from right to left.

It does

It is not the selection that triggers the display of matching items but the position of the insertion point, so left/right or right/left does not, matter just where the cursor is. There is, in fact, no need to select the item to be matched. Just position the insertion point after the item to be matched.

IDE 1.0.5 on Windows 7