Shop OBEX P1 Docs P2 Docs Learn Events
How the IF statement works — Parallax Forums

How the IF statement works

DufferDuffer Posts: 374
edited 2007-04-05 00:28 in BASIC Stamp
I have a question on exactly how the IF statement evaluates expressions. Specifically:

Given: x and y·= 0, do both expressions in the following line get evaluated before a True/False condition for the statement is detirmined.

IF x and y THEN GOSUB DoSomething

My question: When the first expression evauates to FALSE, does the second expression get evaluated or does the IF statement take the FALSE branch without having to evaluate the second expression.

To look at this question another way: Does the execution time of the instruction depend on the relative placement of expressions where you have AND or OR conditions.

I.E. IF NOT x OR NOT y THEN GOSUB DoSomething

Again, does the second expression get evaluated even though it doesn't need to be?

Steve

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-04 00:12
    That's a very good question. In 'C' we call this "short circuit" evaluation -- if the first expression of an "and" is false, the second expression doesn't even have to be evaluated.

    I don't know the answer to this one. I suppose you COULD do a lab on it.

    Do a "IF X THEN" 1000 times. Do a SEROUT before and after, and time it with a stopwatch.

    Then, do an "IF X AND Y THEN" 1000 times, and see what the difference is.

    Of course, you COULD do an

    IF X THEN
    ...
    ELSE IF Y THEN
    ...
    and enforce your own 'short-circuit' evaluation.

    I suspect, the way the BS2 is 'tokenized', that the interpreter won't get to the "GOTO" until after it's already evaluated all the expressions. But it's a great question.
  • DufferDuffer Posts: 374
    edited 2007-04-04 03:07
    I was at work when I posed this question.·On my way home it occurred to me that I could probably use the Basic Stamp Logic Analyzer and toggle a pin each time through a tight loop as you described where only one expression needs to be analyzed and then where both need to be analyzed. If there's a difference in the pulse width between the two, then it must be, as you said, short circuiting.

    I'll post the results if I get this to work.

    Steve
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-04 03:45
    The PBasic tokenizer (compiler) evaluates the Boolean expression to a true/false (non-zero/zero) value, then does the IF as a conditional goto.
  • DufferDuffer Posts: 374
    edited 2007-04-04 03:55
    Mike,
    I understand that concept that you described for a simple one-element expression, but I was wondering if the same was true for complex expressions (multiple Boolean tests connected by AND or OR). Are you saying that everything between the "IF" and the "THEN" is evaluated to a true/false conclusion and then the conditional goto is done based on that result?
    Thanks, Steve
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-04 04:53
    Yes

    The tokenizer (compiler) is not fancy. You can always explicitly write the equivalent of a "short-cut" Boolean evaluation with several IF statements.

    Post Edited (Mike Green) : 4/4/2007 4:57:40 AM GMT
  • DufferDuffer Posts: 374
    edited 2007-04-04 05:00
    Mike,
    Thanks for your insight, knowledge and willingness to share. I just wish you could be a bit less verbose in your responses. wink.gif
    Steve

    And then you had to do the post-edit! cool.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-04 05:08
    Steve,
    When I answer a question, I try to keep in mind that others may be reading this thread who might benefit from a bit more information. Sometimes it works and sometimes it doesn't. Sorry if it seems verbose.
    Mike
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2007-04-04 06:12
    Ummm, I think Steve was calling your "Yes" verbose in jest.· As many on this forum, I read your posts with great interest, and learn much while doing it.
    Steve, your member image is just toooo creepy.

    -Martin
    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-04 13:05
    Eww. It blinks! There was a horror movie not too long ago that used that device.
  • DufferDuffer Posts: 374
    edited 2007-04-04 15:21
    Sorry if I didn't make it clear that I was joking about your one word response. I'm more accustomed to you responses being very detailed, complete and well thought out (and I appreciate that, as I'm sure many others do).
    Steve
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-04 15:51
    No problem. I got it when I finally saw your reply.
  • DufferDuffer Posts: 374
    edited 2007-04-05 00:28
    I did get the BSLA to work for measuring instruction timing. For those that might be interested (but I can't imagine why you would be, I just wanted to put this to bed), here are the results:
    Code used: with variations
    FOR counter = 1 TO 1000
    IF x AND y THEN GOTO true
    true:
    TOGGLE 10
    NEXT
    



    With x and y = 1

    IF x························ 1.28 ms

    IF x·AND y················1.44 ms

    IF x OR y·················· 1.44 ms

    IF NOT x AND NOT y··· 1.47 ms

    With x and y = 0

    IF x························ 1.17 ms

    IF x·AND y················1.32 ms

    IF x OR y·················· 1.32 ms

    IF NOT x AND NOT y··· 1.47 ms

    Conclusions:

    1. Mike was right (no surprise there), no shortcut in PBasic

    2. Expressions that evaluate to FALSE are quicker (who knew?)

    3. Using NOT in expressions takes longer (would have guessed)

    We can all sleep better now that we know these fascinating facts.··roll.gif
Sign In or Register to comment.