Home AMX User Forum AMX General Discussion
Options

Problem with programming manual?

Hello,

I was just reading the AMX programming manual (PDF in this zip file: http://www.amx.com/webtraining/intro_amx_prog/Intro_AMX_Programming.zip) to explain Netlinx to a friend, and noticed what appears to be a problem. Pages 34 and 35 state:
When you are nesting IF... ELSE statements, be sure to use braces. Look at the following
incorrect example:

IF(X = 5)
{
   Statement 1A
   IF(Y = 10)
   {
      Statement 1B
   }
}
ELSE
{
   Statement 2
}

You can see by the alignment of the ELSE statement with the IF(X = 5) that these two should be
associated. The second IF statement is not supposed to have an ELSE counterpart in this example.
However, such is not the case. The Master pairs the second IF statement with the ELSE, because
the compiler associates the ELSE with the ?closest? IF statement. With braces, you can force the
compiler to associate the ELSE with IF(X = 5). For example:

IF(X = 5)
{
   Statement 1A
   IF(Y = 10)
      Statement 1B
}
ELSE
{
   Statement 2
}

By using the braces, you isolate the IF(Y = 10) statement from the IF... ELSE set of
statements.

This is wrong. The first example is correct in isolating the nested IF statement from the "root level" IF...ELSE pair. The two examples should be reversed. Right? If not, I've really been doing things wrong the past few years...

Comments

  • Options
    frthomasfrthomas Posts: 176
    My reading confirms yours, the examples seem reversed.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    I believe the manual is wrong because both examples will yield the same results.

    The ELSE will always be associated with the top IF not matter what kind of conditions, braces or statements are nested in the top IF.

    The braces with the nested IF are optional if there is only to be one statement to be executed. The braces allow for a compound statements to be executed if the IF condition is true.
  • Options
    Something is definitely wrong. Glad it's not just me. ;)

    If someone from AMX reads this, you might want to fix that manual.
  • Options
    frthomasfrthomas Posts: 176
    Yep... It should be

    IF(X = 5)
    {
    ___IF(Y = 10)
    ______Statement 1B
    }
    ELSE
    {
    ___Statement 2
    }

    vs

    IF(X = 5)
    ___IF(Y = 10)
    ______Statement 1B
    ELSE
    {
    ___Statement 2
    }

    Only then do the braces on the first IF prevent the ELSE to be associated with the second IF.
    Oh well, just use braces all the time...

    Fred
  • Options
    Intro to Programming Manual - Fixed

    Thanks for catching the error - we have updated the manual to show the correction. Please view the attachment to see the corrections that were made.

    AMX University
  • Options
    frankly, i always use brackets, even for one liner if statements.

    it will help avoid these type of problems and may even make the code more readable.

    takes a second to add them, can save you hours :)

    it takes no extra resources from the compiler or final application to go the extra effort as well.
Sign In or Register to comment.