Jump to content
Sign in to follow this  
VenusProject2

Strange behavior of a very simple script

Recommended Posts

VenusProject2

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?

Share this post


Link to post
Share on other sites
VenusProject2

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

Share this post


Link to post
Share on other sites
232showtime

still the same.....


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.

Share this post


Link to post
Share on other sites
JohnOne

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.

  • Like 1

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
mikell

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
 

Share this post


Link to post
Share on other sites
VenusProject2

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

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
VenusProject2

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
 

Share this post


Link to post
Share on other sites
VenusProject2

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

Share this post


Link to post
Share on other sites
JohnOne

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.

  • Like 1

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
232showtime

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.

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites
Chimp

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


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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites
mikell

"The For loop terminates when the value of variable exceeds the stop threshold."

 

So my philistine hypothesis was finally not so wrong  :)

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  

×