Is there a "FOR/NEXT" loop problem ?

Discussion in 'Questions (Windows Mobile)' started by dan kabestan, Dec 15, 2007.

  1. dan kabestan

    dan kabestan Member Licensed User

    Would you see the following script sample :
    i put 2 cascaded loops and the second one must
    modify the limit of the first. So :

    Sub Globals
    'listbox1 declared.
    'tn = 0
    'tm = 0
    End Sub
    Sub App_Start
    End Sub
    Sub test
    For n = 0 To tn
    For m = 0 To tm
    If tm<7 Then : tm = tm+1 : End If
    If tn<100 Then : tn = tn+1 : End If
    listbox1.Add("n=" & n & " tn=" & tn & " tm=" & tm)
    listbox1.Add("fin ---- n=" & n & " tn=" & tn & " tm=" & tm)
    End Sub

    Result, (with tn and tm global or not) :
    tm is modified, BUT, tn is not !
    (a '51' stop is curious !)
    Is it a kind of ADA-like safety limit ?
    Sure, one solution is to replace first loop by a DO/WHILE,
    but is it really normal ? where is my wrong ?
    Thank's for help
  2. Erel

    Erel Administrator Staff Member Licensed User

    The upper limit (or lower limit if step < 0) is only calculated when the program enters the loop.
    Changing it inside the for loop will not affect the number of iterations.
  3. dan kabestan

    dan kabestan Member Licensed User

    :sign0087: Right, recorded! (it was not evident)
    thanks for fast answer.