How the IF statement works
Duffer
Posts: 374
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
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
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.
I'll post the results if I get this to work.
Steve
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
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
Thanks for your insight, knowledge and willingness to share. I just wish you could be a bit less verbose in your responses.
Steve
And then you had to do the post-edit!
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
Steve, your member image is just toooo creepy.
-Martin
·
Steve
Code used: with variations
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.··