For…Next Loop in Visual Basic .Net

You can write the For…Next loop in visual basic in following way

For intCount = 1 To 5
Console.WriteLine("Count is now" + intCount.ToString())
Next

intCount is called the control variable
Here loop is going from 1 to 5 continuously
You can start the loop from any number. Here I have used 1
You can use the Step as below to do the increment by 2

For intCount = 2 To 10 Step 2
Console.WriteLine("Count is now" + intCount.ToString())
Next

Looping Backward
If you want to run the loop backward you have to select value of Step less than zero
Look at the following code

For intCount = 10 To 2 Step -2
Console.WriteLine("Count is now" + intCount.ToString())
Next

Leave a Reply

Your email address will not be published. Required fields are marked *