Bagel Posted September 1, 2019 Posted September 1, 2019 I am trying to learn about Select and Case usage with Do loops and I'm having a very difficult time understanding my results. When I run this code using the hotkey the first case executes twice but I can't see how. It's as if the Select statement pulls 0 as the value for $variable every time but then uses the new value as it increments within the case. expandcollapse popup#include <WinAPISysWin.au3> #include <WindowsConstants.au3> #include <Inet.au3> #Include <File.au3> #include <FileConstants.au3> #include <WinAPIFiles.au3> #include <TrayConstants.au3> #include <Date.au3> #pragma compile(inputboxres, true) HotKeySet("!t", "Main") While 1 Sleep(50) WEnd Func Main() $variable = 0 CaseTest( $variable ) EndFunc Func CaseTest ( ByRef $variable ) Do ConsoleWrite( "$variable, " & $variable & ". BEGIN SELECT STATEMENT." & @CRLF ) Select Case $variable < 1 < 2 ConsoleWrite( "The condition for the FIRST case returned true and $variable, " & $variable & ", should still be 0." & @CRLF ) Do ConsoleWrite( "$variable, " & $variable & ", should still be 0 here." & @CRLF ) $variable = $variable + 1 ConsoleWrite( "$variable, " & $variable & ", should now be 1." & @CRLF ) Until $variable = 1 ConsoleWrite( "The Do loop in the FIRST case is complete and $variable, " & $variable & ", should be 1." & @CRLF ) Case 0 < $variable < 2 ConsoleWrite( "The condition for the SECOND case returned true and $variable, " & $variable & ", should still be 1." & @CRLF ) Do ConsoleWrite( "$variable, " & $variable & ", should still be 1 here." & @CRLF ) $variable = $variable + 1 ConsoleWrite( "$variable, " & $variable & ", should now be 2." & @CRLF ) Until $variable = 2 ConsoleWrite( "The Do loop in the SECOND case is complete and $variable, " & $variable & ", should be 2." & @CRLF ) Case 0 < 1 < $variable ConsoleWrite( "The condition for the THIRD case returned true and $variable, " & $variable & ", should still be 2." & @CRLF ) Do ConsoleWrite( "$variable, " & $variable & ", should still be 2 here." & @CRLF ) $variable = $variable + 1 ConsoleWrite( "$variable, " & $variable & ", should now be 3." & @CRLF ) Until $variable = 3 ConsoleWrite( "The Do loop in the THIRD case is complete and $variable, " & $variable & ", should be 3." & @CRLF ) EndSelect ConsoleWrite( "$variable, " & $variable & ". END OF SELECT STATEMENT." & @CRLF ) Until $variable = 3 ConsoleWrite( "$variable, " & $variable & ". END OF TOP DO LOOP." & @CRLF & @CRLF ) EndFunc ; CaseTest
Bagel Posted September 1, 2019 Author Posted September 1, 2019 Just to clarify a bit.. I understand why the Do loop in the first Case recycles infinitely. However, the second time around the first statement in that case reports that the function KNOWS that $variable = 1 BEFORE the Do loop is executed. The only way I can see for that to happen is if "1 < 1" returns TRUE.
jchd Posted September 1, 2019 Posted September 1, 2019 You're misusing the comparison operator. $variable < 1 < 2 Starting with $variable = 0, the expression evaluates from left to right: $variable < 1 < 2 0 < 1 < 2 (0 < 1) < 2 True < 2 The < operator forces conversion to numeric here 1 < 2 which yields True which itself evaluates to 1 At the next iteration, $variables = 1 but: $variable < 1 < 2 1 < 1 < 2 (1 < 1) < 2 False < 2 The < operator forces conversion to numeric here 0 < 2 which yields True which itself evaluates to 1 (again) That's why you never exit the loop albeit $variable increases to signed 64-bit infinity. You want Switch $variable Case 0 ... Case 1 ... Case 2 ... Case 3 ... Case Else ... EndSwitch 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)
Bagel Posted September 1, 2019 Author Posted September 1, 2019 Actually, I really needed to use the compound comparison. After playing with it I found I could achieve the result I was hoping for by using parentheses and "And". My mind was really bent on a more compact form though as the code I'm using this logical structure for is a lot more involved. It's ironic that the simplest way I could think of to test how this all worked ended up being the obstacle to my understanding it. Thanks for the help!
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