[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 12. Statements

Statements may be a simple constant expression or a complicated list of nested loops and conditional statements.

Control statements such as `if`, `while`, and so on control the flow of execution in Octave programs. All the control statements start with special keywords such as `if` and `while`, to distinguish them from simple expressions. Many control statements contain other statements; for example, the `if` statement contains another statement which may or may not be executed.

Each control statement has a corresponding end statement that marks the end of the end of the control statement. For example, the keyword `endif` marks the end of an `if` statement, and `endwhile` marks the end of a `while` statement. You can use the keyword `end` anywhere a more specific end keyword is expected, but using the more specific keywords is preferred because if you use them, Octave is able to provide better diagnostics for mismatched or missing end tokens.

The list of statements contained between keywords like `if` or `while` and the corresponding end statement is called the body of a control statement.

12.1 The `if` Statement |  |
12.2 The `switch` Statement |  |
12.3 The `while` Statement |  |
12.4 The `do-until` Statement |  |
12.5 The `for` Statement |  |
12.6 The `break` Statement |  |
12.7 The `continue` Statement |  |
12.8 The `unwind_protect` Statement |  |
12.9 The `try` Statement |  |
12.10 Continuation Lines |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.1 The `if` Statement

The `if` statement is Octave's decision-making statement. There are three basic forms of an `if` statement. In its simplest form, it looks like this:

    if (condition)
      then-body
    endif

condition is an expression that controls what the rest of the statement will do. The then-body is executed only if condition is true.

The condition in an `if` statement is considered true if its value is non-zero, and false if its value is zero. If the value of the conditional expression in an `if` statement is a vector or a matrix, it is considered true only if all of the elements are non-zero.

The second form of an if statement looks like this:

    if (condition)
      then-body
    else
      else-body
    endif

If condition is true, then-body is executed; otherwise, else-body is executed.

Here is an example:

    if (rem (x, 2) == 0)
      printf ("x is even\n");
    else
      printf ("x is odd\n");
    endif

In this example, if the expression `rem (x, 2) == 0` is true (that is, the value of `x` is divisible by 2), then the first `printf` statement is evaluated, otherwise the second `printf` statement is evaluated.

The third and most general form of the `if` statement allows multiple decisions to be combined in a single statement. It looks like this:

    if (condition)
      then-body
    elseif (condition)
      elseif-body
    else
      else-body
    endif

Any number of `elseif` clauses may appear. Each condition is tested in turn, and if one is found to be true, its corresponding body is executed. If none of the conditions are true and the `else` clause is present, its body is executed. Only one `else` clause may appear, and it must be the last part of the statement.

In the following example, if the first condition is true (that is, the value of `x` is divisible by 2), then the first `printf` statement is executed. If it is false, then the second condition is tested, and if it is true (that is, the value of `x` is divisible by 3), then the second `printf` statement is executed. Otherwise, the third `printf` statement is performed.

    if (rem (x, 2) == 0)
      printf ("x is even\n");
    elseif (rem (x, 3) == 0)
      printf ("x is odd and divisible by 3\n");
    else
      printf ("x is odd\n");
    endif

Note that the `elseif` keyword must not be spelled `else if`, as is allowed in Fortran. If it is, the space between the `else` and `if` will tell Octave to treat this as a new `if` statement within another `if` statement's `else` clause. For example, if you write

    if (c1)
      body-1
    else if (c2)
      body-2
    endif

Octave will expect additional input to complete the first `if` statement. If you are using Octave interactively, it will continue to prompt you for additional input. If Octave is reading this input from a file, it may complain about missing or mismatched `end` statements, or, if you have not used the more specific `end` statements (`endif`, `endfor`, etc.), it may simply produce incorrect results, without producing any warning messages.

It is much easier to see the error if we rewrite the statements above like this,

    if (c1)
      body-1
    else
      if (c2)
        body-2
      endif

using the indentation to show how Octave groups the statements. See section 13. Functions and Script Files.

Built-in Variable: warn_assign_as_truth_value
  If the value of `warn_assign_as_truth_value` is nonzero, a warning is issued for statements like

      if (s = t)
        ...

  since such statements are not common, and it is likely that the intent was to write

      if (s == t)
        ...

  instead.

  There are times when it is useful to write code that contains assignments within the condition of a `while` or `if` statement. For example, statements like

      while (c = getc())
        ...

  are common in C programming.

  It is possible to avoid all warnings about such statements by setting `warn_assign_as_truth_value` to 0, but that may also let real errors like

      if (x = 1)  # intended to test (x == 1)!
        ...

  slip by.

  In such cases, it is possible suppress errors for specific statements by writing them with an extra set of parentheses. For example, writing the previous example as

      while ((c = getc()))
        ...

  will prevent the warning from being printed for this statement, while allowing Octave to warn about other assignments used in conditional contexts.

  The default value of `warn_assign_as_truth_value` is 1.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.2 The `switch` Statement

The `switch` statement was introduced in Octave 2.0.5. It should be considered experimental, and details of the implementation may change slightly in future versions of Octave. If you have comments or would like to share your experiences in trying to use this new command in real programs, please send them to octave-maintainers@bevo.che.wisc.edu. (But if you think you've found a bug, please report it to bug-octave@bevo.che.wisc.edu.

The general form of the `switch` statement is

    switch expression
      case label
        command_list
      case label
        command_list
      ...

      otherwise
        command_list
    endswitch

- The identifiers `switch`, `case`, `otherwise`, and `endswitch` are now keywords.

- The label may be any expression.

- Duplicate label values are not detected. The command_list corresponding to the first match will be executed.

- You must have at least one `case label command_list` clause.

- The `otherwise command_list` clause is optional.

- As with all other specific `end` keywords, `endswitch` may be replaced by `end`, but you can get better diagnostics if you use the specific forms.

- Cases are exclusive, so they don't `fall through' as do the cases in the switch statement of the C language.

- The command_list elements are not optional. Making the list optional would have meant requiring a separator between the label and the command list. Otherwise, things like

      switch (foo)
        case (1) -2
        ...

  would produce surprising results, as would

      switch (foo)
        case (1)
        case (2)
          doit ();
        ...

  particularly for C programmers.

- The implementation is simple-minded and currently offers no real performance improvement over an equivalent `if` block, even if all the labels are integer constants. Perhaps a future variation on this could detect all constant integer labels and improve performance by using a jump table.

Built-in Variable: warn_variable_switch_label
  If the value of this variable is nonzero, Octave will print a warning if a switch label is not a constant or constant expression

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.3 The `while` Statement

In programming, a loop means a part of a program that is (or at least can be) executed two or more times in succession.

The `while` statement is the simplest looping statement in Octave. It repeatedly executes a statement as long as a condition is true. As with the condition in an `if` statement, the condition in a `while` statement is considered true if its value is non-zero, and false if its value is zero. If the value of the conditional expression in a `while` statement is a vector or a matrix, it is considered true only if all of the elements are non-zero.

Octave's `while` statement looks like this:

    while (condition)
      body
    endwhile

Here body is a statement or list of statements that we call the body of the loop, and condition is an expression that controls how long the loop keeps running.

The first thing the `while` statement does is test condition. If condition is true, it executes the statement body. After body has been executed, condition is tested again, and if it is still true, body is executed again. This process repeats until condition is no longer true. If condition is initially false, the body of the loop is never executed.

This example creates a variable `fib` that contains the first ten elements of the Fibonacci sequence.

    fib = ones (1, 10);
    i = 3;
    while (i <= 10)
      fib (i) = fib (i-1) + fib (i-2);
      i++;
    endwhile

Here the body of the loop contains two statements.

The loop works like this: first, the value of `i` is set to 3. Then, the `while` tests whether `i` is less than or equal to 10. This is the case when `i` equals 3, so the value of the `i`-th element of `fib` is set to the sum of the previous two values in the sequence. Then the `i++` increments the value of `i` and the loop repeats. The loop terminates when `i` reaches 11.

A newline is not required between the condition and the body; but using one makes the program clearer unless the body is very simple.

See section 12.1 The `if` Statement, for a description of the variable `warn_assign_as_truth_value`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.4 The `do-until` Statement

The `do-until` statement is similar to the `while` statement, except that it repeatedly executes a statement until a condition becomes true, and the test of the condition is at the end of the loop, so the body of the loop is always executed at least once. As with the condition in an `if` statement, the condition in a `do-until` statement is considered true if its value is non-zero, and false if its value is zero. If the value of the conditional expression in a `do-until` statement is a vector or a matrix, it is considered true only if all of the elements are non-zero.

Octave's `do-until` statement looks like this:

    do
      body
    until (condition)

Here body is a statement or list of statements that we call the body of the loop, and condition is an expression that controls how long the loop keeps running.

This example creates a variable `fib` that contains the first ten elements of the Fibonacci sequence.

    fib = ones (1, 10);
    i = 2;
    do
      i++;
      fib (i) = fib (i-1) + fib (i-2);
    until (i == 10)

A newline is not required between the `do` keyword and the body; but using one makes the program clearer unless the body is very simple.

See section 12.1 The `if` Statement, for a description of the variable `warn_assign_as_truth_value`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.5 The `for` Statement

The `for` statement makes it more convenient to count iterations of a loop. The general form of the `for` statement looks like this:

    for var = expression
      body
    endfor

where body stands for any statement or list of statements, expression is any valid expression, and var may take several forms. Usually it is a simple variable name or an indexed variable. If the value of expression is a structure, var may also be a list. See section 12.5.1 Looping Over Structure Elements, below.

The assignment expression in the `for` statement works a bit differently than Octave's normal assignment statement. Instead of assigning the complete result of the expression, it assigns each column of the expression to var in turn. If expression is a range, a row vector, or a scalar, the value of var will be a scalar each time the loop body is executed. If var is a column vector or a matrix, var will be a column vector each time the loop body is executed.

The following example shows another way to create a vector containing the first ten elements of the Fibonacci sequence, this time using the `for` statement:

    fib = ones (1, 10);
    for i = 3:10
      fib (i) = fib (i-1) + fib (i-2);
    endfor

This code works by first evaluating the expression `3:10`, to produce a range of values from 3 to 10 inclusive. Then the variable `i` is assigned the first element of the range and the body of the loop is executed once. When the end of the loop body is reached, the next value in the range is assigned to the variable `i`, and the loop body is executed again. This process continues until there are no more elements to assign.

Although it is possible to rewrite all `for` loops as `while` loops, the Octave language has both statements because often a `for` loop is both less work to type and more natural to think of. Counting the number of iterations is very common in loops and it can be easier to think of this counting as part of looping rather than as something to do inside the loop.

12.5.1 Looping Over Structure Elements |  |

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 12.5.1 Looping Over Structure Elements

A special form of the `for` statement allows you to loop over all the elements of a structure:

    for [ val, key ] = expression
      body
    endfor

In this form of the `for` statement, the value of expression must be a structure. If it is, key and val are set to the name of the element and the corresponding value in turn, until there are no more elements. For example,

    x.a = 1
    x.b = [1, 2; 3, 4]
    x.c = "string"
    for [val, key] = x
      key
      val
    endfor

         -| key = a
         -| val = 1
         -| key = b
         -| val =
         -|
         -|   1  2
         -|   3  4
         -|
         -| key = c
         -| val = string

The elements are not accessed in any particular order. If you need to cycle through the list in a particular way, you will have to use the function `struct_elements` and sort the list yourself.

The key variable may also be omitted. If it is, the brackets are also optional. This is useful for cycling through the values of all the structure elements when the names of the elements do not need to be known.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.6 The `break` Statement

The `break` statement jumps out of the innermost `for` or `while` loop that encloses it. The `break` statement may only be used within the body of a loop. The following example finds the smallest divisor of a given integer, and also identifies prime numbers:

    num = 103;
    div = 2;
    while (div*div <= num)
      if (rem (num, div) == 0)
        break;
      endif
      div++;
    endwhile
    if (rem (num, div) == 0)
      printf ("Smallest divisor of %d is %d\n", num, div)
    else
      printf ("%d is prime\n", num);
    endif

When the remainder is zero in the first `while` statement, Octave immediately breaks out of the loop. This means that Octave proceeds immediately to the statement following the loop and continues processing. (This is very different from the `exit` statement which stops the entire Octave program.)

Here is another program equivalent to the previous one. It illustrates how the condition of a `while` statement could just as well be replaced with a `break` inside an `if`:

    num = 103;
    div = 2;
    while (1)
      if (rem (num, div) == 0)
        printf ("Smallest divisor of %d is %d\n", num, div);
        break;
      endif
      div++;
      if (div*div > num)
        printf ("%d is prime\n", num);
        break;
      endif
    endwhile

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.7 The `continue` Statement

The `continue` statement, like `break`, is used only inside `for` or `while` loops. It skips over the rest of the loop body, causing the next cycle around the loop to begin immediately. Contrast this with `break`, which jumps out of the loop altogether. Here is an example:

    # print elements of a vector of random
    # integers that are even.

    # first, create a row vector of 10 random
    # integers with values between 0 and 100:

    vec = round (rand (1, 10) * 100);

    # print what we're interested in:

    for x = vec
      if (rem (x, 2) != 0)
        continue;
      endif
      printf ("%d\n", x);
    endfor

If one of the elements of vec is an odd number, this example skips the print statement for that element, and continues back to the first statement in the loop.

This is not a practical example of the `continue` statement, but it should give you a clear understanding of how it works. Normally, one would probably write the loop like this:

    for x = vec
      if (rem (x, 2) == 0)
        printf ("%d\n", x);
      endif
    endfor

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.8 The `unwind_protect` Statement

Octave supports a limited form of exception handling modelled after the unwind-protect form of Lisp.

The general form of an `unwind_protect` block looks like this:

    unwind_protect
      body
    unwind_protect_cleanup
      cleanup
    end_unwind_protect

Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are guaranteed to be executed regardless of how control exits body.

This is useful to protect temporary changes to global variables from possible errors. For example, the following code will always restore the original value of the built-in variable `warn_fortran_indexing` even if an error occurs while performing the indexing operation.

    save_warn_fortran_indexing = warn_fortran_indexing;
    unwind_protect
      warn_fortran_indexing = 1;
      elt = a (idx)
    unwind_protect_cleanup
      warn_fortran_indexing = save_warn_fortran_indexing;
    end_unwind_protect

Without `unwind_protect`, the value of warn_fortran_indexing would not be restored if an error occurs while performing the indexing operation because evaluation would stop at the point of the error and the statement to restore the value would not be executed.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.9 The `try` Statement

In addition to unwind_protect, Octave supports another limited form of exception handling.

The general form of a `try` block looks like this:

    try
      body
    catch
      cleanup
    end_try_catch

Where body and cleanup are both optional and may contain any Octave expressions or commands. The statements in cleanup are only executed if an error occurs in body.

No warnings or error messages are printed while body is executing. If an error does occur during the execution of body, cleanup can use the function `lasterr` to access the text of the message that would have been printed. This is the same as `eval (try, catch)` but it is more efficient since the commands do not need to be parsed each time the try and catch statements are evaluated. See section 14. Error Handling, for more information about the `lasterr` function.

Octave's try block is a very limited variation on the Lisp condition-case form (limited because it cannot handle different classes of errors separately). Perhaps at some point Octave can have some sort of classification of errors and try-catch can be improved to be as powerful as condition-case in Lisp.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 12.10 Continuation Lines

In the Octave language, most statements end with a newline character and you must tell Octave to ignore the newline character in order to continue a statement from one line to the next. Lines that end with the characters `...` or `\` are joined with the following line before they are divided into tokens by Octave's parser. For example, the lines

    x = long_variable_name ...
        + longer_variable_name \
        - 42

form a single statement. The backslash character on the second line above is interpreted a continuation character, not as a division operator.

For continuation lines that do not occur inside string constants, whitespace and comments may appear between the continuation marker and the newline character. For example, the statement

    x = long_variable_name ...     # comment one
        + longer_variable_name \   # comment two
        - 42                       # last comment

is equivalent to the one shown above. Inside string constants, the continuation marker must appear at the end of the line just before the newline character.

Input that occurs inside parentheses can be continued to the next line without having to use a continuation marker. For example, it is possible to write statements like

    if (fine_dining_destination == on_a_boat
        || fine_dining_destination == on_a_train)
      seuss (i, will, not, eat, them, sam, i, am, i,
             will, not, eat, green, eggs, and, ham);
    endif

without having to add to the clutter with continuation markers.

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by John W. Eaton on September, 21 2004 using texi2html