10.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 nonzero, 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 it is non-empty and all of the elements are nonzero.
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.
© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/The-do_002duntil-Statement.html