<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - DO
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="div+en">Previous</a>&nbsp;
<a href="else+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
DO
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>DO</b> [ <b>WHILE</b> <u>Condition</u> ]
&nbsp;&nbsp;&nbsp;&nbsp;.
&nbsp;&nbsp;&nbsp;&nbsp;.
&nbsp;&nbsp;&nbsp;&nbsp;.
&nbsp;&nbsp;[ <b>BREAK</b> | <b>CONTINUE</b> ]
&nbsp;&nbsp;&nbsp;&nbsp;.
&nbsp;&nbsp;&nbsp;&nbsp;.
&nbsp;&nbsp;&nbsp;&nbsp;.
<b>LOOP</b> [ <b>UNTIL</b> <u>Condition</u> ]</pre><p>

Repeats a number of statements while the initial condition remains true or until the final condition becomes true.
<p>
<table class="table" border="0" bordercolor="#000000" cellpadding="4" cellspacing="0">
<tr><th>
Part
</th><th>
Description
</th></tr>
<tr class="dark"><td valign="top">
DO
</td><td valign="top">
Always the first statement of the loop.
</td></tr>
<tr><td valign="top">
<a href="while+en">WHILE</a>
</td><td valign="top">
If used, states a <u>Condition</u> that must remain true to execute the loop.
</td></tr>
<tr class="dark"><td valign="top">
<a href="until+en">UNTIL</a>
</td><td valign="top">
If used, states a <u>Condition</u> that has to become true to stop execution of the loop.
</td></tr>
<tr><td valign="top">
<u>Condition</u>
</td><td valign="top">
Any boolean expression.
</td></tr>
<tr class="dark"><td valign="top">
<a href="break+en">BREAK</a>
</td><td valign="top">
Immediately jumps out of the loop and continues execution of the program with the next line after the loop.
</td></tr>
<tr><td valign="top">
<a href="continue+en">CONTINUE</a>
</td><td valign="top">
Immediately leaves out all following statements in the loop and jumps to the end of the loop causing it to start all over again.
</td></tr>
<tr class="dark"><td valign="top">
<a href="loop+en">LOOP</a>
</td><td valign="top">
Always the last statement of the loop.
</td></tr>
</table>
<p>

<div class="warning"><table class="none" border="0"><tr><td width="40" valign="top"><img border="0" src="../../img/info.png" align="center"></td><td valign="top">
If the initial <u>Condition</u> is false to begin with, the loop is not executed at all. Otherwise, the loop will be executed at least once, even if the final <u>Condition</u> is true to begin with.
</td></tr></table></div>
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' A very simple loop

a = 1

DO WHILE a &lt;= 5
  PRINT &quot;Hello World&quot;; a
  INC a
LOOP
<hr>Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5</pre>
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' The same effect with UNTIL

DO
  PRINT &quot;Hello World&quot;; a
  INC a
LOOP UNTIL a = 6</pre>
<p>
<div class="warning"><table class="none" border="0"><tr><td width="40" valign="top"><img border="0" src="../../img/warning.png" align="center"></td><td valign="top">
Be careful not to enter the <a href="until+en">UNTIL</a> loop with &quot;a&quot; being larger than 5. &quot;a&quot; will be incremented to a larger value than 6, and the only way to stop the loop is lost. You might be better off to use &quot;<a href="loop+en">LOOP</a> <a href="until+en">UNTIL</a> a > 5&quot; instead to minimize the risk of infinite loops.
</td></tr></table></div>
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' This loop will never reach its end value
a = 1

DO WHILE a &lt;= 5
  PRINT &quot;Hello World&quot;; a
  INC a
  IF a = 4 THEN BREAK
LOOP
<hr>Hello World 1
Hello World 2
Hello World 3</pre>
<p>
<hr><b>See also</b><br>
<a href="../cat/loop+en">Loop Control Structures</a>&nbsp;

</div>
</body>
</html>

