question about a loop and for-to

BlitzMax Forums/BlitzMax Beginners Area/question about a loop and for-to

Mindfield(Posted 2006) [#1]
Hi,

This small piece of code about loops confuses me. I understand everything else, except the order why it prints out 1,1 , 1,2 , 1,3 , 1,4 , 2,1 , 2,2 etc. If someone could cut the code to pieces, why it only adds the j-parametre to a higher number, when it's running the 1,1 , 1,2-cycle. I can understand that when the j-parametre reaches 4, it adds one to k and starts that 2,1 , 2,2-thing.

Strict
Local k,j
#Labell
For k=1 To 4
#Label2
For j=1 To 4
Print k+ "," +j
If k=3 Exit Labell
Next
Next

Hopefully I didn't write too confusingly.


tonyg(Posted 2006) [#2]
Not sure what you are asking so what are you expecting to see?
J=4 so inner loop is satisfied, K=3 so we exit #lable1 (the for k=1 to 4 loop).
What we see is...
K=1 J=1 then 2 then 3 then 4
K=2 J=1 then 2 then 3 then 4
K=3 J=1 then hits the if k=3 exit label1 statement.


Mindfield(Posted 2006) [#3]
So there was another loop. It's just that I found this example in BM's Help->Language->Program Flow->Exit and Continue-section, where it described #Label2 as unused in the example. That left me wondering how j keeps getting updated till 4, since there's only one loop. I tought it would go like 1,1 , 2,2 , 3,3.


tonyg(Posted 2006) [#4]
There are 2 for/next loops but only 1 label is used.
Don't get the labels confused with the loops. The labels are simply jump-points. If they're not used thewn they might as well not exist.
Indenting the code shows the 2 loops....
Strict
Local k,j
#Labell
For k=1 To 4
      #Label2
      For j=1 To 4
            Print k+ "," +j
            If k=3 Exit Labell
      Next
Next



Russell(Posted 2006) [#5]
The example was showing the feature of the Exit command where you can jump out to a specified level using labels: But labels are not required if you just want to exit the current loop.

Label2 is not used in that particular example, but it could be:

Strict
Local k,j
#Labell
For k=1 To 4
      #Label2
      For j=1 To 4
            Print k+ "," +j
            If k=3 Exit Labell
            If j=3 Exit Label2
      Next
Next


The label just marks the start of the loop to exit from. BTW, the Exit command does not jump TO that label, but to the end of that loop (Next).

Russell