Contents Previous Next Subchapters

Transferring Control Using The Goto Statement
Syntax label label name
goto  
label name
See Also if , break , halt

Description
The goto statement transfers execution to the point specified by label name.

The goto and label statements must be within a block (a begin/end pair).

Tutorial
The goto statement can be used to calculate a factorial, as was done in the examples of for and while statements. For example, to calculate 5!, you would enter
     fact = 1
     i    = 1
     begin
          label loop
          fact = fact * i
          i    = i + 1
          if i <= 5 then goto loop
     end
     print fact
to which O-Matrix will respond
     120
You can use the goto statement to jump out of a for or while loop. If you enter
     begin
          for i = 1 to 5 begin
               if i > 3 then goto done
               print i
          end
          label done
     end
O-Matrix will respond
     1
     2
     3