Sign in to follow this  
Followers 0
Klaatu

Do logical expressions short-circuit?

10 posts in this topic

#1 ·  Posted (edited)

I searched the forum and the help file but could not find the answer to my question.

Do logical expressions short-circuit? Hopefully you know what I mean.

I've seen some UDFs written (specifically the one I'm looking at is the IsPressed Library UDF by FireFox) as if they do, so was curious if these UDFs need to be modified.

Here's an example:

$ia_R = DllCall($vDLL, 'int', 'GetAsyncKeyState', 'int', '0x' & $iHexKey)
If (@Error = 0) And (BitAND($ia_R[0], 0x8000) = 0x8000) Then Return True

If the DllCall function fails the $ia_R may not be an array, so the reference to the variable in the next statement would error (or at least fail to work as intended) since it uses array subscripting. Correct? But if logical expressions short-circuit the second part of the AND test would not execute since Error would not be zero. Hopefully I've explained myself adequately.

Edited by Klaatu

My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

Share this post


Link to post
Share on other sites



I'm pretty sure they short circuit. An easy way to test is this.

If True And MsgBox(0, "", "") Then Exit

If the message box appears, you know that it is not short circuited. If it does not appear, then it does short circuit.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Yes autoit does use short-circuit (also known by 'lazy-evaluation' :x ) when evaluating a logical expression. For those that don't know what this means, a short explanation:

When evaluating:

condition1 AND condition2 --> If condition1 is FALSE it doesn't check condition2 and the whole condition is FALSE

condition1 OR condition2 --> If condition1 is TRUE it doesn't check condition2 and the whole condition is TRUE.

Even though autoit has instructions like ExitLoop I prefer -to practise good programming habits- to use the short circuit and a boolean flag ($result) like in this function:

Func CompareStringwithArray($string, $array)
    ;function that checks if an element of the array is equal to a string
    Local $ind, $result
    $ind = 0
    $result = False
    While $ind < UBound($array) And Not $result
        If StringCompare($string, $array[$ind]) = 0 Then
            $result = True
        EndIf
        $ind = $ind + 1
    WEnd
    Return $result
EndFunc   ;==>CompareStringwithArray

Obviously, in some cases like using For..In..Next (iterating elements of a set) you have no other choice but to use the ExitLoop.

Edited by Mithrandir

Share this post


Link to post
Share on other sites

klaatu,

what is the prob/issue?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I forgot to adress this part of the question:

Here's an example:

$ia_R = DllCall($vDLL, 'int', 'GetAsyncKeyState', 'int', '0x' & $iHexKey)
If (@Error = 0) And (BitAND($ia_R[0], 0x8000) = 0x8000) Then Return True

If the DllCall function fails the $ia_R may not be an array, so the reference to the variable in the next statement would error (or at least fail to work as intended) since it uses array subscripting. Correct? But if logical expressions short-circuit the second part of the AND test would not execute since Error would not be zero. Hopefully I've explained myself adequately.

Indeed as you say If @error <> 0 then the second part of the condition is not checked and so $ia_R[0] is not accessed (which would cause an error in running time). Edited by Mithrandir

Share this post


Link to post
Share on other sites

klaatu, jaberwocky6669,

If you are trying to determine whether or not a key is pressed then you need both tests. GetAsyncKeyState sets the most significant bit to indicate that the key is down at the time of the call. As for the code logic, you will need a "false" leg to this processing also.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

The help file is explicit about ANDed/ORed conditions short-circuit, even if it mention that without its own topic. You can read about that point in Language Reference > Operators (bottom of page).

Since the behavior is officially documented, it's unlikely to change anytime soon as that could break too many scripts around.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

The help file is explicit about ANDed/ORed conditions short-circuit, even if it mention that without its own topic. You can read about that point in Language Reference > Operators (bottom of page).

Since the behavior is officially documented, it's unlikely to change anytime soon as that could break too many scripts around.

Ah, thanks. I had missed that when searching the help file. Very good.

I'm not interested in it changing, just knowing what the rules are.

Thanks again. Case closed.


My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0