VenusProject2 Posted September 8, 2014 Share Posted September 8, 2014 Do For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) Next MsgBox(0,"DoLoop", " $q = " & $q); Why does $q=8 here? Until $q = 7 $q is incremented from 7 to 8 when finishing the "For/Next" loop, I do not understand why. Can anyone explain this I'm baffled? Link to comment Share on other sites More sharing options...
JohnOne Posted September 8, 2014 Share Posted September 8, 2014 I do not know, but I know that a variable like that should not be used outside the loop it is intended for. 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...
VenusProject2 Posted September 8, 2014 Author Share Posted September 8, 2014 (edited) Good point John, but it's kind of Twilight Zone behavior that is troubling wouldn't you say? This worked.... Do For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) $k = $q Next MsgBox(0,"DoLoop", " $q = " & $q); Why does $q=8 here? Until $k = 7 Edited September 8, 2014 by VenusProject2 Link to comment Share on other sites More sharing options...
232showtime Posted September 8, 2014 Share Posted September 8, 2014 still the same..... ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon. Link to comment Share on other sites More sharing options...
JohnOne Posted September 8, 2014 Share Posted September 8, 2014 I would say it is actually a bug, but someone would come along and say otherwise so let's just say using such a temp variable outside of its loop causes undefined behaviour. I imagine it has something to do with how AutoIt handles loops, and specifically nested loops. You see the Until keyword does not recognize that the variable has reached its limit and Do loop should end. 232showtime 1 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...
Solution mikell Posted September 8, 2014 Solution Share Posted September 8, 2014 Hypothesis : $q is created automatically with Local scope, the start value is 1, the stop value is 7, the step value is 1 Then the For loop tests $q, then increments it, then tests again 7 = stop value then $q = 7 is accepted 8 > stop value then $q = 8 causes an exitloop This means that the 8 value (stop value + step value) must be tested It works with $k = 7 because the tested var is $q, not $k Link to comment Share on other sites More sharing options...
VenusProject2 Posted September 8, 2014 Author Share Posted September 8, 2014 Yes John until otherwise resolved, "using such a temp variable outside of its loop causes undefined behaviour". 232showtime yes still the same disturbing behavior but the work around got the script to work as I needed, albeit with some unnecessary complexity... Link to comment Share on other sites More sharing options...
jchd Posted September 8, 2014 Share Posted September 8, 2014 There is nothing close to a bug there, just basic careless code. 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...
VenusProject2 Posted September 8, 2014 Author Share Posted September 8, 2014 Thanks mikell that explains the behavior, science saves me from believing in something mystical again Hypothesis : $q is created automatically with Local scope, the start value is 1, the stop value is 7, the step value is 1 Then the For loop tests $q, then increments it, then tests again 7 = stop value then $q = 7 is accepted 8 > stop value then $q = 8 causes an exitloop This means that the 8 value (stop value + step value) must be tested It works with $k = 7 because the tested var is $q, not $k Link to comment Share on other sites More sharing options...
VenusProject2 Posted September 8, 2014 Author Share Posted September 8, 2014 Do For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) Next MsgBox(0,"DoLoop", " $q = " & $q) Until $q = 8 This is will do nicely Link to comment Share on other sites More sharing options...
JohnOne Posted September 8, 2014 Share Posted September 8, 2014 I'm not sure I'd accept the answer you did unless I heard it from a code dev. Why would the local var need to be tested again? it is equal to 7 and tested at the top of the loop, it is not While $var < 8 where it might be tested. 232showtime 1 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...
jchd Posted September 8, 2014 Share Posted September 8, 2014 What is the purpose the outer Do loop? 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...
232showtime Posted September 8, 2014 Share Posted September 8, 2014 (edited) I'm not sure I'd accept the answer you did unless I heard it from a code dev. Why would the local var need to be tested again? it is equal to 7 and tested at the top of the loop, it is not While $var < 8 where it might be tested. yeah you are rigth, its kinda strange script... EDIT: Waiting for a Dev. to show up... >_< Edited September 8, 2014 by 232showtime ill get to that... i still need to learn and understand a lot of codes Correct answer, learn to walk before you take on that marathon. Link to comment Share on other sites More sharing options...
kylomas Posted September 8, 2014 Share Posted September 8, 2014 Run it like this and get the same result... ;Do For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) Next MsgBox(0,"DoLoop", " $q = " & $q); Why does $q=8 here? ;Until $q = 7 So the question might be "how is the increment variable being handled?" 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...
mikell Posted September 8, 2014 Share Posted September 8, 2014 (edited) JO, please note that I said Hypothesis Though I'm totally ignorant about how the internal engine works, I can't imagine the way the For loop will 'know' when to exitloop without doing some kind of internal test or comparison My hypothesis is certainly simplistic but tries to find a rational answer For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) ; If $q = 7 Then Exitloop Next MsgBox(0,"", " $q = " & $q) Edit Obviously the Do...Until $q... in this case is bad practice (and redundant) Edited September 8, 2014 by mikell Link to comment Share on other sites More sharing options...
JohnOne Posted September 8, 2014 Share Posted September 8, 2014 I'm not knocking the answer, It's an answer like anyone else's, just saying I would not accept it as the definitive answer. If you tried to use a variable like that in C/++ it will not allow you.. for (int i = 0; i <= 7; i++) { // blah } cout << i; It will error, so there is obviously some handling going on, and only an AutoIt code dev can really say whether this issue is intended or a bug. 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...
Gianni Posted September 8, 2014 Share Posted September 8, 2014 always seen this behavior in "for next" loops, also in other basic languages. This is the normal behavior of the "for next" loop. If you read well the documentation, is also clearly written there: "The For loop terminates when the value of variable exceeds the stop threshold." Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
kylomas Posted September 8, 2014 Share Posted September 8, 2014 Nice, chimp....K.I.S.S. 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...
JohnOne Posted September 8, 2014 Share Posted September 8, 2014 Anyway, a workaround might be... Do For $q = 1 to 7 MsgBox(0,"ForLoop", " $q = " & $q) Next MsgBox(0,"DoLoop", " $q = " & $q); Why does $q=8 here? Until $q > 6 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...
mikell Posted September 8, 2014 Share Posted September 8, 2014 "The For loop terminates when the value of variable exceeds the stop threshold." So my philistine hypothesis was finally not so wrong 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