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"?
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?
}
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.
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
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.