Sign in to follow this  
Followers 0
MrSergey

Problem with For ... to... next

3 posts in this topic

Simple code

For $i = 1 to 10

Next

MsgBox(0,"",$i)

Result = 11

Why?

Share this post


Link to post
Share on other sites



Simple. The loop executes while $i is less or not equal to 10. Therefor it doesn't quit until $i=11 (however the code inside the loop doesn't get executed when $i=11).


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Simple code

Result = 11

Why?

Simple. The loop executes while $i is less or not equal to 10. Therefor it doesn't quit until $i=11 (however the code inside the loop doesn't get executed when $i=11).

Consider the following further examples:
For $i = 1 To 10
    ConsoleWrite("Inside For/Next loop:  $i = " & $i & @LF)
Next
ConsoleWrite("After For/Next loop:  $i = " & $i & @LF & @LF)


$i = 0
Do
    $i += 1
    ConsoleWrite("Inside Do/Until loop:  $i = " & $i & @LF)
Until $i = 10
ConsoleWrite("After Do/Until loop:  $i = " & $i & @LF & @LF)


$i = 0
While 1
    $i += 1
    ConsoleWrite("Inside While/WEnd loop:  $i = " & $i & @LF)
    If $i >= 10 Then ExitLoop
WEnd
ConsoleWrite("After While/WEnd loop:  $i = " & $i & @LF & @LF)

Note you could add "If $i >= 10 Then ExitLoop" to the bottom of the For/Next loop also and get execution on $i = 10, and it will still be 10 after the loop.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0