Go Down

Topic: Nested If/Else (Read 1 time) previous topic - next topic

RFEngineer

Feb 09, 2013, 05:57 pm Last Edit: Feb 09, 2013, 08:01 pm by RFEngineer Reason: 1
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

legendlcox

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.

PaulS

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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

alah

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
}
}

CrossRoads

I find it helps to put in comments, helps to see your logic flow too:
Code: [Select]

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Sometimes its easier to use switch:case also:
Code: [Select]

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)

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

lloyddean

Code: [Select]

if ( ... conditional(s) ... )
{
    if ( ... conditional(s) ... )
    {
        if ( ... conditional(s) ... )
        {
            .... code ...
        }
        else
        {
            .... code ...
        }
    }
    else
    {
        .... code ...
    }
}
else
{
    .... code ...
}


Go Up