Contents Previous Next Subchapters

While Statement
Syntax whilevalue block
See Also block , for , goto , if

Description
Repeats execution of block while value is true, where value is a logical scalar.

Tutorial
If you enter
     x = 1
     while x < 4 begin
          print x
          x = x + 1
     end
O-Matrix will respond
     1
     2
     3
A variable defined before the while statement can be used to accumulate a result. For example, to calculate 5! enter
     fact = 1
     i    = 1
     while i <= 5 begin
          fact = fact * i
          i    = i + 1
     end
     print fact
which would result in
     120
You can nest a while statement within another loop. For example,
     i = 2
     while i > 0 begin
          j = 5
          while j < 10 begin
               print "i =", i, "; j =", j
               j = j + 3
          end
          i = i - 1
     end
will result in,
     i = 2 ; j = 5
     i = 2 ; j = 8
     i = 1 ; j = 5
     i = 1 ; j = 8