n4n0

Nested loop

5 posts in this topic

#1 ·  Posted

I have been trying to move around the values etc, but nothing seems to be working. In this case $i goes up to 18, but I want it to go up to $vLoop1 * $vLoop2 which is 10. Thank you.

Global $vLoop1, $vLoop2, $i

While $vLoop2 <= 2
$vLoop1 = 0
$vLoop2 += 1
While $vLoop1 <= 5

$i += 1
MsgBox(0, "", $i)

$vLoop1 += 1
WEnd
WEnd

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

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?

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

 

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