<html lang="en"> <head> <title>The @code{do-until} Statement - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Statements.html#Statements" title="Statements"> <link rel="prev" href="The-_0040code_007bwhile_007d-Statement.html#The-_0040code_007bwhile_007d-Statement" title="The @code{while} Statement"> <link rel="next" href="The-_0040code_007bfor_007d-Statement.html#The-_0040code_007bfor_007d-Statement" title="The @code{for} Statement"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="The-%3ccode%3edo-until%3c%2fcode%3e-Statement"></a> <a name="The-_003ccode_003edo_002duntil_003c_002fcode_003e-Statement"></a> <p> Next: <a rel="next" accesskey="n" href="The-_003ccode_003efor_003c_002fcode_003e-Statement.html#The-_003ccode_003efor_003c_002fcode_003e-Statement">The <code>for</code> Statement</a>, Previous: <a rel="previous" accesskey="p" href="The-_003ccode_003ewhile_003c_002fcode_003e-Statement.html#The-_003ccode_003ewhile_003c_002fcode_003e-Statement">The <code>while</code> Statement</a>, Up: <a rel="up" accesskey="u" href="Statements.html#Statements">Statements</a> <hr> </div> <h3 class="section">10.4 The <code>do-until</code> Statement</h3> <p><a name="index-g_t_0040code_007bdo_002duntil_007d-statement-568"></a> The <code>do-until</code> statement is similar to the <code>while</code> 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 <code>if</code> statement, the condition in a <code>do-until</code> 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 <code>do-until</code> statement is a vector or a matrix, it is considered true only if it is non-empty and <em>all</em> of the elements are non-zero. <p>Octave's <code>do-until</code> statement looks like this: <pre class="example"> do <var>body</var> until (<var>condition</var>) </pre> <p class="noindent">Here <var>body</var> is a statement or list of statements that we call the <dfn>body</dfn> of the loop, and <var>condition</var> is an expression that controls how long the loop keeps running. <p>This example creates a variable <code>fib</code> that contains the first ten elements of the Fibonacci sequence. <pre class="example"> fib = ones (1, 10); i = 2; do i++; fib (i) = fib (i-1) + fib (i-2); until (i == 10) </pre> <p>A newline is not required between the <code>do</code> keyword and the body; but using one makes the program clearer unless the body is very simple. </body></html>