# Strange behavior of a very simple script

Go to solution Solved by mikell,

## Recommended Posts

```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 on other sites

I do not know, but I know that a variable like that should not be used outside the loop it is intended for.

Monkey's are, like, natures humans.

##### Share on other sites

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 on other sites

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.

##### Share on other sites

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.

Monkey's are, like, natures humans.

##### Share on other sites

• Solution

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 on other sites

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 on other sites

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.
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 on other sites

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

##### Share on other sites

```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 on other sites

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.

Monkey's are, like, natures humans.

##### Share on other sites

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.
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 on other sites

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

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

##### Share on other sites

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 on other sites

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 on other sites

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.

Monkey's are, like, natures humans.

##### Share on other sites

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

##### Share on other sites

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 on other sites

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

Monkey's are, like, natures humans.

##### Share on other sites

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

So my philistine hypothesis was finally not so wrong

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...