do Statement
A
do loop is one way to repeat a section of code more than once.
After each time the loop is executed, the condition is checked.
If the condition is
true, then the loop body starts over from the top.
Because the condition is checked after the loop body, a
do loop will always execute at least once.
Syntax
do
<BODY>
loop while <CONDITION>
- <BODY> is the code to execute repeatedly.
- <CONDITION> is a Boolean expression indicating whether the loop should repeat for another iteration.
Usage
Consider the following example.
sub Main()
dim num as Number
do
print "Enter a number: ";
input num
loop while num <> 0
end sub
The execution looks like the following flow chart.
↓
┌───────────────────────────┐
┌─>│ print "Enter a number: "; │
│ └───────────────────────────┘
│ ↓
│ ┌───────────┐
│ │ input num │
│ └───────────┘
│ ↓
│ ┌──────┐
└───────────│ num? │
num <> 0 └──────┘
↓ num = 0
This is similar to a
while loop.
In a
do loop, the condition is checked
after running the loop body.
In a
while loop, the condition is checked
before running the loop body.
This means that a
do loop will always execute at least once, but a
while loop can execute zero times.