Next Previous Contents

4. Program Control Flow

This section covers the aspects of the language that control which program statements get executed, and in what order. Control statements do not evalute to a numeric value. The available control statements are the if-statement, the while-statement, and the for-statement. A break-statement and a continue-statement offer program execution control from within the if, for, and while statements.

4.1 If Statement

The if-statement performs a test on the expression in parenthesis, expr, and executes the statements enclosed within braces, if the expression is true (has a non-zero value). The expression must evaluate to a scalar-expression, otherwise a run-time error will result.

if ( expr ) { statements }

The user is free to insert newlines for formatting purposes. expr can be the simplest expression, a constant, or something more complex, like an assignment, function call, or relational test(s). Starting with a simple example:

> if ( 1 ) { "TRUE" }
TRUE
> if ( 0 ) { "TRUE" }

An optional else keyword is allowed to delineate statements that will be executed if the expression tests false:

> if ( 0 ) { "TRUE" else "FALSE" }
FALSE

An explicit else-if keyword is not available, however, the else-if control flow can be reproduced with nested if-statments.

> if ( 0 )
  {
    "true-1"
  else if ( 0 ) {
    "true-2"
  else if ( 1 ) {
    "true-3"
  else
    "else-part"
  }}}
true-3  

4.2 While Statement

The while-statement executes the body of statements until the scalar expr is false (has a zero value).

while ( expr ) { statements }

The while statement is useful when the loop termination conditions are not know a-priori. When the loop termination condition is know prior to execution, a for-loop is more efficient. An often used example is reading a data file, line by line until the end-of-file is reached:

> while (length (ans = getline ("file")))
  {
    # Operate on the file contents...
  }

4.3 For Statement

The for-statement executes the body of statements for each element of vector. The first time the loop-body is executed var is set the value of the first element of vector. The loop is re-executed for each element of vector with var set to each subsequent value of vector. If vector is empty, then the loop-body is not executed.

for ( var in vector ) { statements }

The for-loop vector can be any type of vector: numeric, either real or complex, or string. Quite often the for-loop vector is constructed on the fly using vector notation. Some simple examples:

> n = 2;
> for ( i in 1:n ) { printf ("%i ", i); } printf("\n");
1 2 
> x = ["a", "sample", "string"];
> for ( i in x ) { printf ("%s ", i); } printf("\n");
a sample string 

The first part of the previous example shows how a for-loop vector is often constructed on the fly. The second part demonstrates how a string vector can be used in a for-loop.

4.4 Break Statement

The break statement allows program execution to be transfered out of a while or for statement. Consequently, break statements are only valid within for and while loops. When the break statement is executed, execution of the inner-most loop terminates.

> for ( i in 1:100 ) { if (i == 3) { break } } i
        3  

4.5 Continue Statement

The continue statement forces execution of the next iteration of the inner-most for or while loop to begin immediately. Consequently, continue statements are only valid within for or while loops.

> for ( i in 1:4 ) { if (i == 2) { continue } i }
        1  
        3  
        4  


Next Previous Contents