Pages: [1]   Go Down
Author Topic: Nested If/Else  (Read 761 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: February 09, 2013, 02:01:08 pm by RFEngineer » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3466
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Venezuela
Offline Offline
Jr. Member
**
Karma: 2
Posts: 74
The less You explain, the more I understand
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I find it helps to put in comments, helps to see your logic flow too:
Code:
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
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23718
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sometimes its easier to use switch:case also:
Code:
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)
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if ( ... conditional(s) ... )
{
    if ( ... conditional(s) ... )
    {
        if ( ... conditional(s) ... )
        {
            .... code ...
        }
        else
        {
            .... code ...
        }
    }
    else
    {
        .... code ...
    }
}
else
{
    .... code ...
}
Logged

Pages: [1]   Go Up
Jump to: