Announcement

Collapse
No announcement yet.

Classic if/then/else ambiguity?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Classic if/then/else ambiguity?

    I was just curious the other day and entered in the following line of code into PB:

    Code:
    if a=q then if a=5 then q=5 else q=6
    To my surprise, this compiled without any problems. This begs the question, to which if statement does the else statement belong?

    Most people will never do this type of thing in one line, instead using the very clear block if statement instead. But I was curious. This is a classic problem in the C language, and most compiles bind the else to the closed if. But that's somewhat arbitrary.

    Michael


    ------------------

  • #2
    Michael,

    The ELSE corresponds to the if a=5. However, I recommend for
    readability that it should be written as follows:

    Code:
    IF a = q THEN
         IF a = 5 THEN
              LET q = 5
         ELSE
              LET q = 6
         END IF
    END IF
    By writing it this way there should not be any ambiguity with what
    corresponds to what. Also, I recommend the usage of indentation as
    well as additional blanks.

    The manner in which you wrote the statement used to be implemted when
    there was a limited amount of RAM in the computer.
    Allen

    ------------------

    Comment


    • #3
      I am well aware of how to properly code a block if statement.

      I do find one line if statements frequently convenient and I use them in C quite a bit. I was merely suprised that pb allowed the nested one-line if statement (given the separate form of the block if), and curious as to how the precedence was resolved. In C this type of thing is present because there is only one form of if statement. In PB there are two (at least as far as grammar is concerned).

      Michael


      ------------------

      Comment


      • #4
        The ELSE applies to the most recent [single-line] IF statement.

        It's generally possible to write convoluted and/or ambiguous code in most platforms, but for the most part you have to try harder to do it in BASIC than in C/++.

        Using a structured approach usually always makes for much more readable code, hence using an IF-block in this case makes perfect sense because the logical-intent of the code is clear to the programmer <U>and</U> the next person who has to read the code in 2 years time.



        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          [/quote]
          The ELSE applies to the most recent [single-line] IF statement
          [/quote]

          In "classic" BASIC (this means I have not tested specifically with PB-brand BASICs), ELSE applies to most recent unmatched IF statement....

          MCM


          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            I was referring specifically to the code above, but you are quite correct.
            Code:
            ' PB/CC code:
            DEFLNG a-z
            FUNCTION PBMAIN
              a = 1
              FOR a = 1 TO 2
                FOR b = 1 TO 2
                  IF a=1 THEN IF b=1 THEN c=1 ELSE c=2 ELSE c=3
                    PRINT a,b,c
              NEXT b,a
            END FUNCTION
             
            ' PB/DOS code:
            DEFINT a-z
            a = 1
            FOR a = 1 TO 2
              FOR b = 1 TO 2
                IF a=1 THEN IF b=1 THEN c=1 ELSE c=2 ELSE c=3
                  PRINT a,b,c
            NEXT b,a
            Both produce the following results:
            Code:
            a&  b&  c&
            ----------
             1   1   1
             1   2   2
             2   1   3
             2   2   3
            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment

            Working...
            X