Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame
In the previous article in this series, "Discovering Visual Basic .NET: Making Decisions", you found out how to make your programs smarter by letting them make their own decisions. In the this article, I'll introduce the topic of looping and show you how to get your program to execute several lines of code again and again.
Visual Basic .NET includes two types of loops. The For...Next loop counts off a certain number of times and then quits. The Do...Loop uses a condition similar to an If...Then statement to determine whether or not it should continue looping each time.
Counting with For...Next
With the For...Next loop, you can easily execute some commands a set number of times while keeping track of how many times you've gone through the loop. Here's an example:
Imports System Imports Microsoft.VisualBasic Module Santa Public Sub Main() Dim Num As Integer Console.WriteLine("Santa is watching you,") For Num = 1 To 3 Console.WriteLine("...and you,") Next Console.WriteLine("...and especially you!") End Sub End Module
This code displays the same line three times. The result looks like this:
Santa is watching you, ...and you, ...and you, ...and you, ...and especially you!
The For line marks the beginning of the loop. For also identifies the index variable (in this case, Num), the number of the first loop (1), and the number of the last loop (3). The index variable holds the number of the current loop.
The line that contains Next marks the end of the loop. Everything between the For line and the Next line is a part of the loop's body—that is, the stuff that gets executed again and again. The first time through the loop, Num is set to 1. The second time, it's set to 2, and the third time, 3.
You can use any Integer variable as an index variable in a For...Next loop. The index variable is simply assigned the loop value each time the For line is executed. Here's an example showing the index value each time:
Imports System Imports Microsoft.VisualBasic Module Counting Public Sub Main() Dim Count As Integer Console.WriteLine("OK boys and girls, count with me!") For Count = 1 To 5 Console.WriteLine(Count) Next Console.WriteLine("Very good!") End Sub End Module
OK boys and girls, count with me! 1 2 3 4 5 Very good!
But remember that changing the index value yourself within the loop is rarely a good idea. The For loop really gets confused when you do that.
Most loops usually start with 1. But they don't have to. You can create a loop like this:
For Items = 10 To 100
This loop sets the variable Items to 10 the first time through, to 11 the second time through, and so on, up to 100. This loop executes 91 times.
Here's another example:
For Counter = 0 To 5
Again, the first time through, Counter is set to 0, then to 1, and so on, up to 5. This loop executes 6 times.
You can even do this:
For Coordinate = -5 To 5
The first time through, Coordinate is set to -5, then to -4, then on up through 0 and ending with 5. This loop executes 11 times (counting 0).
Watch Where You Step
By using the keyword Step with your For...Next loops, you can tell Visual Basic .NET what number the For loop counts by. Here's an example:
For Num = 2 To 10 Step 2
In this loop, the first time through, Num is assigned 2, then 4, then 6, then 8, and finally 10. Here's another example:
For Weeks = 0 To 35 Step 7
Weeks is assigned 0 the first time, then 7, then 14, 21, 28, and 35.
You can even step backward! You do that by specifying a negative number for the step value. Here's an example:
Imports System Imports Microsoft.VisualBasic Module CountDown Public Sub Main() Dim CountDown As Integer For CountDown = 10 To 1 Step -1 Console.WriteLine(CountDown) Next Console.WriteLine("Blast Off!") End Sub End Module
10 9 8 7 6 5 4 3 2 1 Blast Off!