Discovering Visual Basic .NET: Repeating Code
The VB.NET Do...Loop is a very different kind of looping structure from the For...Next loop. Do...Loop enables you to loop while a condition is true or while a condition is false (until it becomes true).
Do While and Do Until
A Do...Loop looks like this:
Imports System Imports Microsoft.VisualBasic Module Gumballs Public Sub Main() Dim Amount As Single Dim Quarters As Integer Amount = 3.85 Quarters = 0 Do Quarters = Quarters + 1 Amount = Amount - .25 Loop While Amount >= .25 Console.WriteLine("You can buy " & Quarters) Console.WriteLine("gumballs at $.25 each") End Sub End Module
You can buy 15 gumballs at $.25 each
As you might expect, the loop begins with Do and ends with Loop. Everything in between is the body of the loop.
You'll notice in this example that Loop is immediately followed by the keyword While. This keyword indicates that a condition will follow and that the loop will continue executing as long as the condition remains true. When the condition is tested and is false, the loop stops repeating.
You can change the Do line to use Until, instead:
Loop Until Amount < .25
Until is the logical opposite of While. If you use Until, the loop continues as long as the condition remains false. When the condition is tested and is true, the loop stops repeating.
So, to change this program to use Until, I have to change the condition so that the program still works the same way.
The gumball counting program has just one problem.
Set Amount to .17. Now try running the program again:
You can buy 1 gumballs at $.25 each
Hmm. It still says you have enough to buy one gumball. And that's not true. You don't have $.25, so the result should be 0. Why did this happen?
Well, when While or Until appears on the Loop line at the bottom of the loop, you can always be sure that the body of the loop will be executed once. That's because the condition after the While or Until isn't checked until you get to the Loop line—after you've gone through all the lines in the body. This is called a bottom-tested loop.
In this case, it counts off one quarter and subtracts the .25 from Amount. Of course, Amount has a negative number at that point, but the quarter has already been added in.
To fix this, you can switch to a top-tested loop. It's easy. Just move the While or Until and the condition to the top of the loop, after the Do keyword:
Imports System Imports Microsoft.VisualBasic Module Gumballs Public Sub Main() Dim Amount As Single Dim Quarters As Integer Amount = .17 Quarters = 0 Do Until Amount < .25 Quarters = Quarters + 1 Amount = Amount - .25 Loop Console.WriteLine("You can buy " > Quarters) Console.WriteLine("gumballs at $.25 each") End Sub End Module
This time, the condition is checked first thing, before the body of the loop ever executes. This loop is to execute until Amount is less than .25. In other words, after the value goes below .25, the loop should stop. And because Amount starts out at .17, the loop stops before it ever starts and none of the lines in the loop get executed. You simply jump over the entire loop and begin with whatever follows Loop:
You can buy 0 gumballs at $.25 each
So, is a top-tested loop always better than a bottom tested loop? Not necessarily. The best method depends on what you're doing and how you want to set it up. In some cases, you want the loop to execute at least once. Use whatever works best for your situation.
Exit, Stage Left
You may discover, right in the middle of a loop, that you want to get out of the loop entirely—no matter what else is happening. VB.NET makes this possible with the Exit command:
For Count = 1 To 100 . . . If Temp > Threshold Then Exit For End If . . . Next
Usually, you find Exit For within an If...Then statement that checks for some special case why the loop needs to end. You can use Exit Do in exactly the same way to exit a Do...Loop.
In this installment, you discovered how to make your programs repeat themselves using loops. This is the last installment for this article series. I hope it has piqued your interest in programming with VB.NET and that it will provide a foundation for your future reading and experimentation with it.
Page 2 of 2