Next Previous Contents

2. Flow of control statements

2.1 if, else

Reserved words: if, else

The if statement syntax:

    if (expr) stmt
or
    if (expr) stmt else stmt

The expression must evaluate to integer scalar or array. The condition is considered true if the scalar is nonzero or if all the array elements are nonzero. Examples:

    if (x!=0)
        format("x is nonzero, x=``\n",x)    // notice no semicolon
    else {
        x = 1;
        format("x was zero and is now 1\n");// this semicolon is optional
    };  // this semicolon separates this if stmt from the next stmt, if any

2.2 for

Reserved word: for

The for statement syntax:

    for (init-stmt; continue-condition-expr; update-stmt) stmt

For example:

    for (i=1; i<=imax; i++) a[i] = 0.5*i;

The for statement is similar to that found in C language. The only exception is that in Tela the 'init-stmt' and 'update-stmt' are statements, whereas in C they are expressions. This is because the assignment '=' is a statement in Tela, not an operator as in C. There is no comma operator in Tela. If you want to have several statements as your init or update statement, you have to use braces, for example:

    for ({i=1; j=10}; i<=10; {i++;j--}) format("i=``, j=``\n",i,j);

When expr is considered true, see if.

2.3 foreach

Reserved word: foreach

The foreach statement syntax:

    foreach (i=A) stmt;
will loop over all components of array A, setting i equal to the component on each iteration.

WARNING: This statement is currently included only for easing transition from Matlab. The implementation is not optimal. In the future the statement may even disappear. The Matlab to Tela translator m2t generates foreach statements, which you should translate to ordinary for statements sooner or later.

See also: for

2.4 repeat, until

Reserved words: repeat, until

The repeat..until statement syntax:

    repeat stmt-sequence until expr

For example:

    i = 1;
    repeat
        format("i = ``\n",i);
        i++
    until i > 10;

This is exactly similar to Pascal, including the use of semicolons. When expr is considered true, see if.

See also: while, if

2.5 while

Reserved word: while

The while statement syntax:

    while (expr) stmt

For example:

    while (!found) {
        LookForIt();
        counter++
    };

The statement is executed until expr evaluates to false. When expr is considered false or true, see if.

See also: repeat

2.6 return

Reserved word: return

The return statement returns from the current function. Any output variables must be assigned before calling return, or they will remain undefined.

If called from the main level of a source'd file or command line, stops execution of the source'd file.

    function result=AllPositive(A) {
        for (i=1; i<=length(A); i++)
                        if (A[i]<=0) {
                                result = 0;
                                return
                        };
                result = 1;
    };

The function returns 1 if all elements of vector A are positive, and zero otherwise.

See also: function, break

2.7 break, continue

Reserved words: break, continue

The break statement exits the surrounding loop. It is analogous to the break statement of C. The loop can be a for-loop, a while-loop or a repeat- until loop.

The continue-statement effectively jumps to the end of the body of the surrounding loop, causing the next iteration to begin. It is analogous to the continue statement of C.

For example,

    for (i=1; i<=10; i++) {
        if (i==5) break;
        disp i
    };
outputs the numbers 1,2,3 and 4. On the other hand,
    for (i=1; i<=10; i++) {
        if (i==5) continue;
        disp i
    };
outputs the numbers from one to ten, excluding five.

See also: return, goto.

2.8 goto, label

Reserved words: goto, label

The label statement defines a named jump address in instruction stream. The goto statement jumps to a given label. The labels must be identifiers. For example,

    for (i=1; i<=10; i++) for (j=1; j<=10; j++)
        if (M[i,j] < 0) {
            format("Negative matrix entry, exiting\n");
            goto exitloop;
        };
    label exitloop;
    // processing continues ...
would be equivalent to
    if (any(M<0)) format("Negative matrix entry, exiting\n");

The goto statements are local to a function. All goto addresses must be defined as labels in the function. Gotos into blocks or loops are currently allowed but may cause unpredictable results.

Think twice before you use goto. In almost all cases it can be avoided by using the return, break, or continue statements.

See also: return, break.


Next Previous Contents