Jump to content

Nested loop


n4n0
 Share

Recommended Posts

Uninititalised counters will start at 0, not 1. Better explicitly define what you want your loops to do.:)

Global $i=0
For $vLoop2=1 to 2 Step 1
    For $vLoop1=1 to 5 Step 1
        $i+=1
        MsgBox(0,"",$i)
    Next
Next

 

Link to comment
Share on other sites

8 minutes ago, RTFC said:

Uninititalised counters will start at 0, not 1. Better explicitly define what you want your loops to do.:)

Global $i=0
For $vLoop2=1 to 2 Step 1
    For $vLoop1=1 to 5 Step 1
        $i+=1
        MsgBox(0,"",$i)
    Next
Next

 

I did something similar first, and it worked, but then I ran into problems exiting the loop, ie. if $vLoop1 value changes because of given arguments. How would I rewrite this to use While loop instead?

Link to comment
Share on other sites

For loops are much better imho, not sure what you mean by "I ran into problems exiting the loop", however to do the same using While Wend you could use the following:

Global $vLoop1 = 1, $vLoop2 = 1, $i = 0

While $vLoop2 <= 2
    $vLoop2 += 1
    While $vLoop1 <= 5
        $i += 1
        ConsoleWrite($i & @CRLF)
        $vLoop1 += 1
    WEnd
    $vLoop1 = 1
WEnd

 

Link to comment
Share on other sites

5 hours ago, n4n0 said:

I ran into problems exiting the loop

:blink: ExitLoop doesn't work for you?

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