next up previous contents index
Next: Questions and answers inside Up: Programs Previous: Structure of programs   Contents   Index


Labels, conditional jumps and loops

A label in a program is a line of the following type

name:

where name is a string of characters (with no '#', '{', '&' or ':') followed by a ':'. It is possible to jump to this line (inside the same program) by using the instruction

goto name

It is possible also to make conditional jumps by testing the value of numerical expressions. The instruction

if> expr name1

will jump to label name1: or not, according to the numerical expression expr. If it is positive the program jumps to name1:, otherwise it goes to the next line. The label must of course exist somewhere in the program. It is also possible to test if an expression is negative or zero, by using

if< expr name1

in the first case, and

if= expr name1

in the second case.


The syntax of loops is as follows :

do var var1 var2 incr
.
.
.
enddo

where var is a variable name, var1, var2 and incr are numerical expressions. It is not necessary to define var before using it in this loop. Initially, var is set to var1 and in each step it if incremented by adding incr. The loop ends when var becomes larger than var2. This will always work if var2 and incr are constant during the loop and incr is positive, and definitely never if var2 is constant and incr negative or zero. The parameter incr can be omitted. In this case it is assumed to be 1. It is possible to nest loops.

In fact there is not a real implementation of loops, but rather a translation of the instructions do..., enddo when the program is loaded, using conditional jumps. It is possible to see this by listing a program containing loops.


Example : Suppose that the file prog.cmd in the command directory contains the following lines :


:example1
3
1
-1
x=0
do i #1 #2
x=x+i*i
enddo

Given two numbers $a$ and $b$ this program computes

\begin{displaymath}\mathop{\hbox{$\displaystyle\sum$}}\limits _{a\leq i\leq b}i^2.\end{displaymath}

The instruction

- interpcom -> proglist example1

prints the following on the screen

x=0
i=#1-1
0:
i=i+1
if> i-(#2) 1
x=x+i*i
goto 0
1:


next up previous contents index
Next: Questions and answers inside Up: Programs Previous: Structure of programs   Contents   Index
2009-11-12