Jump to content

Strange behavior of a very simple script


Go to solution Solved by mikell,

Recommended Posts

Posted

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?

Posted (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 by VenusProject2
Posted

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.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

  • Solution
Posted

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
 

Posted

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...

Posted

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 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)

Posted

Thanks mikell that explains the behavior, science saves me from believing in something mystical again :thumbsup:

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
 

Posted

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 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)

Posted (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 by 232showtime

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Posted

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

Posted (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 by mikell
Posted

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.

Posted

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."

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...