Keyword

# Do Loop

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Repeatedly executes a series of statements while a specified condition is `True</api/language/true>`.

## Usage

``` xojo
Do [ Until Condition ]
  [ Statements ]
  [ Continue [ Do ] ]
  [ Exit [ Do ] ]
  [ Statements ]
Loop [ Until Condition ]
```

| Part                                     | Description                                                                                                                                                                              |
|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Do \[ Until *condition*\]                | Begins the loop. If the optional **Until** and *condition* are included, the *condition* is evaluated to determine if the loop should exit.                                              |
| Condition                                | Any valid `Boolean</api/data_types/boolean>` expression. When this expression evaluates to `True</api/language/true>`, the loop will exit.                                               |
| Statements                               | Optional: Statements to be executed repeatedly (until *condition* evaluates to `False</api/language/false>`).                                                                            |
| `Continue</api/language/loops/continue>` | Optional: If a `Continue</api/language/loops/continue>` statement is present, execution will skip over the remaining statements in the loop and resume with a new iteration of the loop. |
| `Exit</api/language/loops/exit>`         | Optional: If an `Exit</api/language/loops/exit>` statement is present, execution of the loop is terminated and resumes with the next line following the loop.                            |
| Loop \[ Until *condition*\]              | Ends the loop. If the optional **Until** and *condition* are included, the *condition* is evaluated to determine if the loop should exit.                                                |

## Notes

The <span class="title-ref">Do...Loop</span> statement continues to execute statements repeatedly until condition is `True</api/language/true>` or an `Exit</api/language/loops/exit>` statement within the loop is executed.

If *statements* should only be executed if *condition* is already `True</api/language/true>`, test for *condition* at the beginning of the loop. If statements should execute at least once, test *condition* at the end of the loop.

If *condition* is not used to cause the loop to exit, your logic must call the `Exit</api/language/loops/exit>` statement somewhere inside the <span class="title-ref">Do...Loop</span> or the loop will never end.

Do...Loop statements can be nested to any level. Each Loop statement goes with the previous Do statement.

When a loop runs it takes over the interface, preventing the user from interacting with menus and controls. If the loop is very lengthy, you should move the code for the loop to a separate `Thread</api/language/threading/thread>`, allowing it to execute in the background.

You can declare variables using the `Var</api/language/var>` statement within the loop. However, such variables are local to the loop (rather than the method) and go out of scope after the last iteration of the loop. For example,

``` xojo
Var x As Integer
Do Until x > 100
  Var a As Integer
  x = x + 1
  a = x + a
Loop
MessageBox(a.ToString) ' out of scope
```

This code makes sense only if the variable "a" is declared prior to the loop.

## Sample code

This example uses the <span class="title-ref">Do...Loop</span> statement to increment a variable. If the variable is greater than 100 before the loop begins, the variable will not be modified.

``` xojo
Do Until x > 100
  x = x * 2
Loop
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this example, the variable will be modified at least once because *condition* is tested at the end of the loop.

``` xojo
Do
  x = x * 2
Loop Until x > 100
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

More sample code:

``` xojo
Var x As Integer
Do
  x = x + 1
Loop Until x = 100
' x = 100

x = 0
Do
  x = x + 1
  If x >= 100 Then Exit
Loop
' x = 100
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Continue</api/language/loops/continue>`, `Exit</api/language/loops/exit>`, `For...Next</api/language/loops/for...next>`, `While...Wend</api/language/loops/while...wend>` statements; `DesktopApplication</api/user_interface/desktop/desktopapplication>`, `Thread</api/language/threading/thread>` classes.

</div>
