Announcement

Collapse
No announcement yet.

Double NOT statement

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

  • Double NOT statement

    I am having trouble with the NOT statement:

    OPEN SourceFile FOR INPUT AS 100 LEN = 32768
    OPEN FilePathStr+FileNameStr FOR INPUT AS 200 LEN = 32768
    WHILE NOT EOF(100) AND NOT EOF(200) AND Done = 0 THEN
    'my code
    WEND

    I get a syntax error with the While statement. I want to keep looping unles any of the three conditions is met ie: i reach the end of file #100 or i reach the end of file #200 or Done = 1.

    Anyone?

    ------------------
    Kind Regards
    Mike

  • #2
    You don't need a THEN at the end of a WHILE line.

    When the compiler gives you the row and column of an error, that will very often tell you exactly where the problem is.

    It might make the code easier to read if you did this instead:

    Code:
    WHILE NOT (EOF(100) OR EOF(200) OR Done = 1)
    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited June 10, 2001).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      Oh god DUH! - just shoot me.
      I been at this keyboard too long
      Sorry everyone

      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        Or better still, trade in the bitwise operators for boolean (unary) operators which allows PB to use short-circuit evaluation for improved performance...

        Code:
        WHILE ISFALSE EOF(100) AND ISFALSE EOF(200) AND ISFALSE Done


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

        Comment


        • #5
          PowerBASIC is already using short-circuit evaluation here. The key is the use
          of ANDs rather than Eric's parenthesized OR version. However, the speed difference
          won't usually be significant, so it is reasonable to go with whichever you find
          most clear.

          ------------------
          Tom Hanlin
          PowerBASIC Staff

          Comment


          • #6
            > The key is the use of ANDs rather than
            > Eric's parenthesized OR version

            Hmmm... I see your point. I thought that the compiler would use short-circuit evaluation on the stuff inside the parens and then perform the NOT, but of course the parens force the use of bitwise evaluation. That requires the whole line to be evaluated before the NOT.

            > the speed difference won't usually be significant

            Agreed, sometimes readability is more important that a few microseconds of CPU time. But not always!

            Thanks for pointing that out, Tom.

            -- Eric



            ------------------
            Perfect Sync Development Tools
            Perfect Sync Web Site
            Contact Us: mailto:[email protected][email protected]</A>
            "Not my circus, not my monkeys."

            Comment


            • #7
              Sorry I'm on this topic again (in the last weeks there has been other forums on the logic-shortcut topic). I can see the following:

              1- If the speed is not a must, then I prefer readibility.
              2- If the speed is a must, may be the automatic shortcut logic could be a good solution.
              3- There is a way to write the code is BOTH readable AND fast, in some situations even faster than the automatic shortcut.

              The source line
              Code:
              IF A=100 AND EOF(200) AND MY_TEST_FUNCTION(A&) THEN ...
              will be short-cutted. Which operand will be tested first? To get the fastest code I'd know which operand is slower to test, and what probability each operand is true or false. For instance, if MY_TEST_FUNCTION is a very slow function, the fastest code could be:
              Code:
              IF A=100 THEN
                IF EOF(200) THEN
                  IF MY_TEST_FUNCTION(A&) THEN
                    ...
                  END IF
                END IF
              END IF
              This way MY_TEST_FUNCTION will be executed as few times as possible (of course this is good if I don't need the function to be executed every time, otherwise I had to test it first).

              May be the expressions are always evaluated left-to-right, or right-to-left. Will a rule like this be always true? Is a rule like this easy-to-read?

              The code with nested IFs gives me the absolute control on how the original expression will be evaluated and shortcutted. I can execute a series of bench-tests to optimize it. Hence, I can write the fastest code without resigning about the readibility.

              ------------------
              Rgds, Aldo

              Comment


              • #8
                PowerBASIC evaluates from left-to-right, so if the left most condition is FALSE, then the remainder are not evaluated (when short-circuit evaluation is being used).

                On this basis, the best performance would likely occur if the left most condition involved testing a register variable.

                Benchmarking is something you can certainly try, but since you will be measuring a small number of clock cycles, be sure to test on a variety of processors (Intel, AMD, etc) so you are not mislead by processor-specific performance differences.


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

                Comment


                • #9
                  Mike,

                  Unless you are in the middle of an assembler algorithm, you will
                  nost see the speed difference, even with fancy benchmarking so I
                  would go for code clarity.

                  I personally like the triple disjunct that Eric suggested and I
                  would have coded it in much the same way but Lance's example is
                  quite clear so its a matter of taste I guess.

                  The fastest way to do it would be a series of CMP and JMP in asm
                  but unless you really need to do that, the choices presented to
                  you here will do the job fine.

                  Regards,

                  [email protected]

                  ------------------
                  hutch at movsd dot com
                  The MASM Forum

                  www.masm32.com

                  Comment

                  Working...
                  X