Klaatu Posted January 7, 2011 Share Posted January 7, 2011 (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 TrueIf 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 January 7, 2011 by Klaatu My Projects:DebugIt - Debug your AutoIt scripts with DebugIt! Link to comment Share on other sites More sharing options...
Richard Robertson Posted January 7, 2011 Share Posted January 7, 2011 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. Link to comment Share on other sites More sharing options...
Mithrandir Posted January 8, 2011 Share Posted January 8, 2011 (edited) Yes autoit does use short-circuit (also known by 'lazy-evaluation' ) 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 FALSEcondition1 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 ;==>CompareStringwithArrayObviously, in some cases like using For..In..Next (iterating elements of a set) you have no other choice but to use the ExitLoop. Edited January 8, 2011 by Mithrandir Help with SOAP message!! Link to comment Share on other sites More sharing options...
JohnOne Posted January 8, 2011 Share Posted January 8, 2011 But its asking id @error = 0, in which case there was no error with the call so there is an array. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
kylomas Posted January 8, 2011 Share Posted January 8, 2011 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 Link to comment Share on other sites More sharing options...
Mithrandir Posted January 8, 2011 Share Posted January 8, 2011 (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 January 8, 2011 by Mithrandir Help with SOAP message!! Link to comment Share on other sites More sharing options...
jaberwacky Posted January 8, 2011 Share Posted January 8, 2011 But its asking id @error = 0, in which case there was no error with the call so there is an array.So maybe he should only need to test @ERROR? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
kylomas Posted January 8, 2011 Share Posted January 8, 2011 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 Link to comment Share on other sites More sharing options...
jchd Posted January 8, 2011 Share Posted January 8, 2011 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
Klaatu Posted January 8, 2011 Author Share Posted January 8, 2011 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now