Menu

do loop for next

2025-11-06
2025-11-07
  • Pere font vilanova

    I checked these words:
    test1 10 0 do ." i " i . 10 0 do ."j" j . loop loop ; and doesnt work but
    test1 10 0 do ." i" i . 10 0 do ." j" i . loop loop ; it works!
    also
    test2 10 for ." i" i . 10 for ." j"j . next next ; doesnt works but
    test2 10 for ." i" i . 10 for ." j" i . next next ; it works

    that is not standard at all... is it normal?

     
    • Mikael Nordman

      Mikael Nordman - 2025-11-06

      It does not work because the Scamp has used I for the FOR NEXT. Defining I again for DO LOOP is not allowed in FlashForth.

      So, in Scamp I can only be used by FOR NEXT, there I is just an alias for R@. I asked John not to do that, but he did, so now there is this mess with I and DO LOOP.

       
  • Pere font vilanova

    really in the loop do loop i use the index Ix and Jx , in this way ther is no interference with the i and j of the for next......

     
    • Mikael Nordman

      Mikael Nordman - 2025-11-06

      So what was your problem ?

       
  • Pere font vilanova

    in the rest of forths the firts way is the correct, using i and j, not using always i.
    the question is why when i use i and j, the double loop doesnt work well?

     
  • Mikael Nordman

    Mikael Nordman - 2025-11-07

    It is difficult to answer a question if you do not show the real code that you are using. If you use Ix then write it in the code.

    For me the do loop works just fine on the scamp 3e.

    : test1 10 0 do ." i" ii . 10 0 do ." j" j . loop cr loop ; ok
    
    i0 j0 j0 j0 j0 j0 j0 j0 j0 j0 j0 
    i1 j1 j1 j1 j1 j1 j1 j1 j1 j1 j1 
    i2 j2 j2 j2 j2 j2 j2 j2 j2 j2 j2 
    i3 j3 j3 j3 j3 j3 j3 j3 j3 j3 j3 
    i4 j4 j4 j4 j4 j4 j4 j4 j4 j4 j4 
    i5 j5 j5 j5 j5 j5 j5 j5 j5 j5 j5 
    i6 j6 j6 j6 j6 j6 j6 j6 j6 j6 j6 
    i7 j7 j7 j7 j7 j7 j7 j7 j7 j7 j7 
    i8 j8 j8 j8 j8 j8 j8 j8 j8 j8 j8 
    i9 j9 j9 j9 j9 j9 j9 j9 j9 j9 j9 
     ok
    
     
  • Mikael Nordman

    Mikael Nordman - 2025-11-07

    How did you define J for the FOR NEXT? You cannot use J defined for DO LOOP in the FOR NEXT LOOP.

     
  • Pere font vilanova

    hello , i use i and jin the for next loop and ix and jx in the do loop:
    I checked these words:
    test1 10 0 do ." i " ix . 10 0 do ."j" jx . loop loop ; and doesnt work but (a)
    test1 10 0 do ." i" ix . 10 0 do ." j" ix. loop loop ; it works! (b)

    (a) :
    i0 j0 j0 j0 j0 j0 j0 j0 j0 j0 j0
    i1 j1 j1 j1 j1 j1 j1 j1 j1 j1 j1
    i2 j2 j2 j2 j2 j2 j2 j2 j2 j2 j2
    i3 j3 j3 j3 j3 j3 j3 j3 j3 j3 j3
    i4 j4 j4 j4 j4 j4 j4 j4 j4 j4 j4
    i5 j5 j5 j5 j5 j5 j5 j5 j5 j5 j5
    i6 j6 j6 j6 j6 j6 j6 j6 j6 j6 j6
    i7 j7 j7 j7 j7 j7 j7 j7 j7 j7 j7
    i8 j8 j8 j8 j8 j8 j8 j8 j8 j8 j8
    i9 j9 j9 j9 j9 j9 j9 j9 j9 j9 j9
    but this is not correct! must be:
    (b)
    i0 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i1 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i2 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i3 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i4 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i5 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i6 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i7 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i8 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9
    i9 j0 j1 j2 j3 j4 j5 j6 j7 j8 j9

    the same happen with for next loop. if I use the index i and j or ix and jx, the esult is not ok (a) but if I only use the index i or ix in the nested loops then it works well (b).

     

    Last edit: Pere font vilanova 2025-11-07
  • Mikael Nordman

    Mikael Nordman - 2025-11-07

    (a) is correct. You need to think about what the function of the word J is.
    J gets you the value of the index of the outer loop.

     
  • Mikael Nordman

    Mikael Nordman - 2025-11-07
    gforth
    Gforth 0.7.9_20250321
    Authors: Anton Ertl, Bernd Paysan, Jens Wilke et al., for more type `authors'
    Copyright © 2025 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
    Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
    Type `help' for basic help
    : test1 10 0 do ." i" i . 10 0 do ." j" j . loop cr loop ;  ok
    test1 i0 j0 j0 j0 j0 j0 j0 j0 j0 j0 j0 
    i1 j1 j1 j1 j1 j1 j1 j1 j1 j1 j1 
    i2 j2 j2 j2 j2 j2 j2 j2 j2 j2 j2 
    i3 j3 j3 j3 j3 j3 j3 j3 j3 j3 j3 
    i4 j4 j4 j4 j4 j4 j4 j4 j4 j4 j4 
    i5 j5 j5 j5 j5 j5 j5 j5 j5 j5 j5 
    i6 j6 j6 j6 j6 j6 j6 j6 j6 j6 j6 
    i7 j7 j7 j7 j7 j7 j7 j7 j7 j7 j7 
    i8 j8 j8 j8 j8 j8 j8 j8 j8 j8 j8 
    i9 j9 j9 j9 j9 j9 j9 j9 j9 j9 j9 
     ok
    
     
  • Pere font vilanova

    oh oh totally true, i had a wrong idea. really i is inner loop and j is outer loop. thank you again.....

     
  • Pere font vilanova

    curiously if i use only i . the nested loop runs ok........

     
    • Mikael Nordman

      Mikael Nordman - 2025-11-07

      curiously if i use only i . the nested loop runs ok........

      Why ? That is normal. I accesses the current loop index, J the outer loop index, K 2 levels up.

       
  • Pere font vilanova

    yes but normally in nested loop i used:
    qq 10 0 do Jx . 10 0 do I x . loop cr loop ; that is working well
    but if i make
    ww 10 0 do Ix . 10 0 do Ix . loop cr loop ; also it works... then why you need Jx?
    qq 10 0 do i . 10 0 do i . loop cr loop ; \ tested on my ipad swiftforth
    \ but also checked in my scamp
    ok
    qq
    0 0 1 2 3 4 5 6 7 8 9
    1 0 1 2 3 4 5 6 7 8 9
    2 0 1 2 3 4 5 6 7 8 9
    3 0 1 2 3 4 5 6 7 8 9
    4 0 1 2 3 4 5 6 7 8 9
    5 0 1 2 3 4 5 6 7 8 9
    6 0 1 2 3 4 5 6 7 8 9
    7 0 1 2 3 4 5 6 7 8 9
    8 0 1 2 3 4 5 6 7 8 9
    9 0 1 2 3 4 5 6 7 8 9
    ok
     

    Last edit: Pere font vilanova 2025-11-07
    • Mikael Nordman

      Mikael Nordman - 2025-11-07

      You need J when you in the inner loop want to use the index of the outer loop.

      : test3 10 0 do  10 0 do ." i" ii . ." j" j . loop cr loop ; ok
      
      test3
      i0 j0 i1 j0 i2 j0 i3 j0 i4 j0 i5 j0 i6 j0 i7 j0 i8 j0 i9 j0 
      i0 j1 i1 j1 i2 j1 i3 j1 i4 j1 i5 j1 i6 j1 i7 j1 i8 j1 i9 j1 
      i0 j2 i1 j2 i2 j2 i3 j2 i4 j2 i5 j2 i6 j2 i7 j2 i8 j2 i9 j2 
      i0 j3 i1 j3 i2 j3 i3 j3 i4 j3 i5 j3 i6 j3 i7 j3 i8 j3 i9 j3 
      i0 j4 i1 j4 i2 j4 i3 j4 i4 j4 i5 j4 i6 j4 i7 j4 i8 j4 i9 j4 
      i0 j5 i1 j5 i2 j5 i3 j5 i4 j5 i5 j5 i6 j5 i7 j5 i8 j5 i9 j5 
      i0 j6 i1 j6 i2 j6 i3 j6 i4 j6 i5 j6 i6 j6 i7 j6 i8 j6 i9 j6 
      i0 j7 i1 j7 i2 j7 i3 j7 i4 j7 i5 j7 i6 j7 i7 j7 i8 j7 i9 j7 
      i0 j8 i1 j8 i2 j8 i3 j8 i4 j8 i5 j8 i6 j8 i7 j8 i8 j8 i9 j8 
      i0 j9 i1 j9 i2 j9 i3 j9 i4 j9 i5 j9 i6 j9 i7 j9 i8 j9 i9 j9 
       ok
      
       
  • Pere font vilanova

    also : tt 10 for i . 10 for i . next cr next ;
    ok
    tt
    10 10 9 8 7 6 5 4 3 2 1 0
    9 10 9 8 7 6 5 4 3 2 1 0
    8 10 9 8 7 6 5 4 3 2 1 0
    7 10 9 8 7 6 5 4 3 2 1 0
    6 10 9 8 7 6 5 4 3 2 1 0
    5 10 9 8 7 6 5 4 3 2 1 0
    4 10 9 8 7 6 5 4 3 2 1 0
    3 10 9 8 7 6 5 4 3 2 1 0
    2 10 9 8 7 6 5 4 3 2 1 0
    1 10 9 8 7 6 5 4 3 2 1 0
    0 10 9 8 7 6 5 4 3 2 1 0
    ok

     
    • Mikael Nordman

      Mikael Nordman - 2025-11-07

      Your code does not match the result in FF. Maybe in gforth ?

      This is correct. What do you think it was supposed to do ?

       : tt 10 for i . 10 for i . next cr next ;
        ok
      tt
      9 9 8 7 6 5 4 3 2 1 0 
      8 9 8 7 6 5 4 3 2 1 0 
      7 9 8 7 6 5 4 3 2 1 0 
      6 9 8 7 6 5 4 3 2 1 0 
      5 9 8 7 6 5 4 3 2 1 0 
      4 9 8 7 6 5 4 3 2 1 0 
      3 9 8 7 6 5 4 3 2 1 0 
      2 9 8 7 6 5 4 3 2 1 0 
      1 9 8 7 6 5 4 3 2 1 0 
      0 9 8 7 6 5 4 3 2 1 0 
       ok
      
       

      Last edit: Mikael Nordman 2025-11-07
  • Pere font vilanova

    I always thought that when there were two nested loops, the index value of each loop had to be requested using I and J.
    But in the last example, I see that I can get the value of each index (inner and outer) using only index I, without needing to use J. I suppose they should only be used if both indices are requested within the same loop.
    sorry for a lot of questions, but i'm beginner forth user....

     

Log in to post a comment.