www.want-tool.org
Using Loops
   

By Andrew J. Wozniewicz
Milwaukee, August 5, 2008

Here is an example showing how to use loops in WANTScript.

There are three kinds of loop constructs available right now: WHILE-loop, REPEAT-loop, and FOR-loop. One difference with Pascal is that the FOR-loop has an optional step clause (alas, not illustrated in the example below).

Also, the keyword loop can be used instead of do, and/or repeat, so that while-do is equivalent to while-loop, for-do is equivalent to for-loop, and repeat-until is equivalent to loop-until. In other words, loop can appear whenever do can appear, and vice versa; likewise, loop can appear whenever repeat can appear, and vice-versa.

project FloydsTriangle //Test190

{
Loops. WHILE-DO, FOR-TO, REPEAT-UNTIL
}

  WriteLn("Floyd's Triangle using WHILE loop")
  FloydsTriangleWHILE(5)
  WriteLn
  WriteLn("Floyd's Triangle using FOR loop")
  FloydsTriangleFOR(5)
  WriteLn
  WriteLn("Floyd's Triangle using REPEAT loop")
  FloydsTriangleUNTIL(5)

  {
    Floyd's triangle is a right angled triangular array of natural numbers.
    It is named after Robert Floyd. It is defined by filling the rows of
    The triangle with consecutive numbers, starting with a 1 in the top
    left corner:

    1
    2 3
    4 5 6
    7 8 9 10
    11 12 13 14 15

    The numbers along the right edge of the triangle are the
    triangular numbers.

    Source: www.wikipedia.org/wiki/Floyd%27s_triangle
  }

  import System.Utilities

  procedure FloydsTriangleFOR(MaxRows)
    var Row, Column
    var Number

    Number = 0
    for Row := 1 to MaxRows loop
      for Column := 1 to Row do
        Number++
        Write( PadLeft(Number,3) )
      end
      WriteLn
    end
  end


  procedure FloydsTriangleWHILE(MaxRows)
    var Row, Column
    var Number

    Number = 0
    Row := 1
    while Row <= MaxRows loop
      Column := 1
      while Column <= Row do
        Number++
        Write( PadLeft(Number,3) )
        Column++
      end
      WriteLn
      Row++
    end
  end


  procedure FloydsTriangleUNTIL(MaxRows)
    var Row, Column
    var Number

    Number = 0
    Row := 1
    loop
      Column := 1
      while Column <= Row do
        Number++
        Write( PadLeft(Number,3) )
        Column++
      end
      WriteLn
      Row++
    until Row > MaxRows
  end

end
Here is the output:
Floyd's Triangle using WHILE loop
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15

Floyd's Triangle using FOR loop
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15

Floyd's Triangle using REPEAT loop
  1
  2  3
  4  5  6
  7  8  9 10
 11 12 13 14 15

-Andrew