Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

For loop acting weird?

Asked by 9 years ago

So I was playing with putting for loops into each other and then suddenly the output kept printing the same loop twice,why?

for a = 10,1,-1 do
     for b = 5,1,-1 do
          for c = 2,1,-1 do
             print([[a : ]]..a..[[  b : ]]..b..[[   c : ]]..c)

         end
   end
end


I don't get why it's printing out the loops twice if I don't have a two on the first loop?

I have a ten so, shouldn't it just printing out ten times?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Okay, let's step through this in a simpler way.

for a = 10,1,-1 do
    print("a = " .. a)
    for b = 5,1,-1 do
        print("\tb = " .. b)
        for c = 2,1,-1 do
            print("\t\tc = " .. c)
        end
    end
end

This is the output we get:

a = 10
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 9
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 8
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 7
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 6
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 5
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 4
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 3
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 2
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1
a = 1
    b = 5
        c = 2
        c = 1
    b = 4
        c = 2
        c = 1
    b = 3
        c = 2
        c = 1
    b = 2
        c = 2
        c = 1
    b = 1
        c = 2
        c = 1

This makes sense.

We get to a for loop. It say, start a at 10. Check (that's the first line of output).

Now we enter the body of the a loop. We start b at 5. Check.

Now we went the body of the b loop. We start c at 2. Check.

Now the body of the c loop finished, so it advances to 1. Check.

Now 1 is where it was supposed to stop. So now we start the second iteration of the b loop. b is 4. Check.

Now we have to execute the body of the b loop; so c is 2, then 1. Check, check.

etc.

Ad

Answer this question