### DO...LOOP

Control flow statement for looping

**Syntax:**

`
`

**Do**[ {

**Until**|

**While**}

*condition*]

[

*statement block*]**Loop**

*or***Do**

[

*statement block*]**Loop**[ {

**Until**|

**While**}

*condition*]

**Description:**

The

If

If an

In the first syntax, the

`statement executes the statements in the following`**Do***until/while the*`statement block`*, if any, evaluates to true.*`condition`If

`is used, the`**Until**`statement stops repetition of the`**Do***when*`statement block`*evaluates to true. The*`condition``keyword has opposite effect, stopping the loop if`**While***condition*evaluates to false. If both*and either*`condition``or`**Until**`are omitted, the`**While**`statement loops indefinitely.`**Do**If an

`Exit``statement is encountered inside the`**Do***, the loop is terminated, and execution resumes immediately following the enclosing*`statement block``statement. If a`**Loop**`Continue``statement is encountered, the rest of the`**Do***is skipped and execution resumes at the*`statement block``statement.`**Do**In the first syntax, the

*is checked when the*`condition``statement is first encountered, and if the`**Do***is met, the*`condition`*will be skipped. In the second syntax,*`statement block`*is initially checked*`condition`*after*the*is executed. This means that the*`statement block`*is always guaranteed to execute*`statement block`*at least*once.*may be any valid expression that evaluates to False (zero) or True (non-zero).*`condition`**Examples:**

In this example, a

`loop is used to count the total number of odd numbers from 1 to 10. It will repeat`**Do**`its`*until*`condition is met:`*n > 10*Dim As Integer n = 1 '' number to check

Dim As Integer total_odd = 0 '' running total of odd numbers

Do Until( n > 10 )

If( ( n Mod 2 ) > 0 ) Then total_odd += 1 '' add to total if n is odd (has remainder from division by 2)

n += 1

Loop

Print "total odd numbers: " ; total_odd '' prints '5'

End 0

Dim As Integer total_odd = 0 '' running total of odd numbers

Do Until( n > 10 )

If( ( n Mod 2 ) > 0 ) Then total_odd += 1 '' add to total if n is odd (has remainder from division by 2)

n += 1

Loop

Print "total odd numbers: " ; total_odd '' prints '5'

End 0

Here, an infinite DO loop is used to count the total number of evens. We place the conditional check inside the loop via an

`If...Then`statement, which exits the loop if and when*becomes true:*`n > 10` Dim As Integer n = 1 '' number to check

Dim As Integer total_even = 0 '' running total of even numbers

Do

If( n > 10 ) Then Exit Do '' exit if we've scanned our 10 numbers

If( ( n Mod 2 ) = 0 ) Then total_even += 1 '' add to total if n is even (no remainder from division by 2)

n += 1

Loop

Print "total even numbers: " ; total_even '' prints '5'

End 0

Dim As Integer total_even = 0 '' running total of even numbers

Do

If( n > 10 ) Then Exit Do '' exit if we've scanned our 10 numbers

If( ( n Mod 2 ) = 0 ) Then total_even += 1 '' add to total if n is even (no remainder from division by 2)

n += 1

Loop

Print "total even numbers: " ; total_even '' prints '5'

End 0

**Dialect Differences:**

- In the
*-lang qb*and*-lang fblite*dialects, variables declared inside a..**Do**block have a function-wide scope as in QB**Loop** - In the
*-lang fb*and*-lang deprecated*dialects, variables declared inside a..**Do**block are visible only inside the block, and can't be accessed outside it.**Loop**

**Differences from QB:**

- None

**See also:**

Back to Control Flow