There are many ways to run through a loop in code, and Visual Basic has some that seem to be superfluous. But not only are there slight differences, when you’re working with loops inside of loops, you can use different types to control the flow.
By using the
Continue command, you can
Continue Do, or
Continue While. The Compiler will know which Loop you’re referring to, and jump to the next iteration of that loop.
This example doesn’t make any sense, but it illustrates nested loops.
While y < 1000
y += 1
For z = 0 to 1000 Step 3
If z = y * 5 Then Continue While
If z = x * y Then Continue Do
x += 1
Loop Until x > 1000
The big difference between
While is that the
Do...Loop format allows you to start or end the Loop with a Conditional, by way of
Do Until or
Loop Until, or you can have no Conditional at all, in which case you have to use
Exit at some point. Whereas the
While...End While format not only requires the Conditional, the Conditional has to be at the start of the loop.
Then of course, if you want to skip out of a Loop, use the
Exit Command, such as
Exit Do, or
Exit While. And as with
Continue, by using different types of Loops, the Compiler automatically knows which one you mean. You can jump out of one or two Loops with one Statement.