Jump to content

Problem with For ... to... next


Recommended Posts

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
Link to comment
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
 Share

  • Recently Browsing   0 members

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