Nested If/Else

I am trying to understand the relationship of the open and closing brackets when using nested IF/ELSE statements. I cannot seem to grasp the concept of exactly when a complete IF/ELSE statement ends. In other languages like COBOL, END-IF marks the end of each IF/ELSE statements in the nest and finally, a period ("."), marks the end of the entire nested IF/ELSE logical argument. I believe my current program is failing because of misplaced brackets - although the program verifies, of course. I've searched many on-line reference but what's needed is a way to look at a flowchart in relation to the code.

So, I have included a link below to a .PDF of a flowchart I did this morning using SmartDraw. It has three nested levels. Although there are probably better ways of managing nested IF/ELSE arguments, like a case statement, I really need to understand the open and closing bracket relationship before I can move forward. Anyone care to reply with the code to match the flowchart in the link? Many thanks in advance!

http://tinyurl.com/b7n6mdp

Here's my shot at it. Right now, I'm just focused on the "{ }" brackets and not the Arduino commands:

If X {
If Y {
If Z {
do D
}
Else {
do C
}
}
Else {
do B
}
}
Else {
do A
}
}

-Paul

If it helps at all inside of the if statement is a loop and if that loop is false it goes to the else. The end of the if else statement is when the last bracket ends.

I find it easier to visualize the range of an if or else block by putting the { on the next line.

Me too, that and using the auto format tool to ensure that indentation is consistent allows me to look up the page from a right brace to see where block of statements began and down from a left brace to see where the block ends. Moving the insertion point by clicking after either kind of brace, or bracket for that matter, highlights the corresponding brace or bracket of the pair which also makes them stand out amongst the code.

Auto format is also great for warning of missing left/right braces/brackets. For instance, the code example in this thread will not auto format because there are "Too many right curly braces" so it give a clue what to look for, although it may actually be too few left curly braces ! Starting the code blocks encompassed by braces with a blank line before and after the braces also helps to spot what is wrong with the code structure.

Unfortunately it is still possible to have code that auto formats and compiles but still doesn't do what you intended.

Hi, RF< what they want to say is; write your code so you can see your braces at the beginning of the NEXT line. If you place your cursor just right ( not LEFT of the braces you´ll see the corresponding RIGHT one ).
If you have 3 left braces in your sketch you HAVE TO have 3 right braces, ALSO.
That´s why you could have “ too MANY right curly braces” OR “ too FEW left curly braces “
Count yours and try writing your code like this:

If X
{
If Y
{
do D
}
Else
{
do C
}
}
Else
{
do B

}
}
Else
{
do A
}
}

I find it helps to put in comments, helps to see your logic flow too:

If X {
  If Y {
    If Z {
      do D
    }// end if Z
    Else {
      do C
    } // end else C
  } // end if Y
  Else {
    do B
  } // end else B
} // end if X
Else {
  do A
} // end else A
// }  this one not needed
1 Like

Sometimes its easier to use switch:case also:

switch(XYX){
case X:
// code
break;
case Y:
// code
break;
case Z:
// code
break;
case A:
// code
break;
case B:
// code
break;
case C:
// code
break;
} // end switch(XYZ)
if ( ... conditional(s) ... )
{
    if ( ... conditional(s) ... )
    {
        if ( ... conditional(s) ... )
        {
            .... code ...
        }
        else
        {
            .... code ...
        }
    }
    else
    {
        .... code ...
    }
}
else
{
    .... code ...
}