0% found this document useful (0 votes)
33 views3 pages

PowerPoint Do... Loop

The Do...Loop statement in Visual Basic allows repeating a block of code while or until a condition is met. It has two basic syntax forms - with the condition appearing before or after the code block. Within the loop, Exit Do can be used to prematurely exit the innermost Do loop. An example demonstrates using nested Do loops, with an inner loop counting to 20 and exiting early if the user responds "No" to a prompt, while the outer loop repeats until the user is finished.

Uploaded by

Zan Moe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

PowerPoint Do... Loop

The Do...Loop statement in Visual Basic allows repeating a block of code while or until a condition is met. It has two basic syntax forms - with the condition appearing before or after the code block. Within the loop, Exit Do can be used to prematurely exit the innermost Do loop. An example demonstrates using nested Do loops, with an inner loop counting to 20 and exiting early if the user responds "No" to a prompt, while the outer loop repeats until the user is finished.

Uploaded by

Zan Moe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Do...

Loop statement
12/03/2018 • 2 minutes to read • +1
In this article
Syntax
Remarks
Example
See also

Repeats a block of statements while a condition is True or until a condition becomes


True.

Syntax
Do [{ While | Until } condition ]
[ statements ]
[ Exit Do ]
[ statements ]
Loop
Or, you can use this syntax:
Do
[ statements ]
[ Exit Do ]
[ statements ]
Loop [{ While | Until } condition ]

The Do Loop statement syntax has these parts:


Part Description

condition Optional. Numeric expression or string expression that is True or False. If


condition is Null, condition is treated as False.

statements One or more statements that are repeated while, or until, condition is True.

Remarks
Any number of Exit Do statements may be placed anywhere in the Do…Loop as an
alternate way to exit a Do…Loop. Exit Do is often used after evaluating some condition,
for example, If…Then, in which case the Exit Do statement transfers control to the
statement immediately following the Loop.
When used within nested Do…Loop statements, Exit Do transfers control to the loop
that is one nested level above the loop where Exit Do occurs.

Example
This example shows how Do...Loop statements can be used. The inner Do...Loop
statement loops 10 times, asks the user if it should keep going, sets the value of the flag
to False when they select No, and exits prematurely by using the Exit Do statement. The
outer loop exits immediately upon checking the value of the flag.

VB = Copy

Public Sub LoopExample()


Dim Check As Boolean, Counter As Long, Total As Long
Check = True: Counter = 0: Total = 0 ' Initialize variables.
Do ' Outer loop.
Do While Counter < 20 ' Inner Loop
Counter = Counter + 1 ' Increment Counter.
If Counter Mod 10 = 0 Then ' Check in with the user on
every multiple of 10.
Check = (MsgBox("Keep going?", vbYesNo) = vbYes) '
Stop when user click's on No
If Not Check Then Exit Do ' Exit inner loop.
End If
Loop
Total = Total + Counter ' Exit Do Lands here.
Counter = 0
Loop Until Check = False ' Exit outer loop immediately.
MsgBox "Counted to: " & Total
End Sub

See also
Using Do...Loop statements
Data types
Statements

Support and feedback


Have questions or feedback about Office VBA or this documentation? Please see Office
VBA support and feedback for guidance about the ways you can receive support and
provide feedback.

Is this page helpful?


 Yes  No

You might also like