VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Advanced Comments
- Compiling Directives
The Loops are block of codes that often will be run more then once at a row. Some of them will run a certain amount of time while others while run depending on the output of a condition. In this Part we will go through 3 of the 5 loops in VB.NET.
The While Loop:
The while loop will continue with its loop as long a condition remains True. The condition is tested at the beginning each time the loop will start a new turn. If the condition isn't met at the beginning the loop won't even run once. The syntax of a while loop looks as following:
While <condition> 'Code that will be run once per turn End While
To show a simple example:
Dim X as Integer = 3 While X<300 X*=2 End While
The above loop will loop while X<300, each time in the loop the value of X is doubled. After some turns X will be doubled from 192 to 383. Then when the loop check if the condition "X<300" is met it will get a False value and therefor the loop will end.
You have to be sure you don't create a infinite loop. An infinite loop is a loop which will never end. The result is that the program will never continue and will stop to response. Here's an infinite loop:
Dim X As Integer = 141 While X < 200 X -= 5 End While
Independening on how many times the loop will loop, the loop won't end. The condition X<200 will always be True since X will only be smaller and smaller for each turn. The program will stop to response.
Do While loop:
This type of loop has only one difference to the While loop more then the syntax. The Do While loop will check the condition at the end instead of in the beginning. This is actually pretty important. Here's the syntax anyway:
Do 'Code that will be run once per turn Loop While <condition>
So why do we sometimes want to use the Do While loop? The reason is that sometimes we wants the loop to be run at least once even though the conditions aren't met.
In the examples below I will use some more functionalities in the messagebox, I add a caption and what buttons it should use, I also check what button that was pressed to use it in an if statement:
Dim nPoints As Integer While nPoints >= 15 nPoints -= 10 If MessageBox.Show("Do you want any points?", "Points=" & nPoints, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then nPoints += 40 End If End While
Do While loop
Dim nPoints As Integer Do nPoints -= 10 If MessageBox.Show("Do you want any points?", "Points=" & nPoints, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then nPoints += 40 End If Loop While nPoints >= 15
Here, in both examples, the loop will continue as long as the user have 15 or more points, the user loses 10 points each time but can gain 40 new if he/she answer yes on the messagebox. But we want the user to be able to play this "game" so then the loop need to loop at least once. The while loop will test the condition in the beginning but since the user starts with 0 points the loop won't start at all. Because of that we use the Do While loop, then the code will run at least once so the "game" can start.
Do Until loop:
The Do Until loop as the Do While loop but with one exception. The Do While loop will continue if the condition are True and stop if the condition are False. The Do Until loop does the opposite, it will continue if the condition are False and stop if the condition are True. It will loop Until the condition are met. The syntax of the Do Until loop and also an example of it:
Do 'Code that will be run once per turn Loop Until <condition>
Dim X As Integer = 312 Do X /= 10 Loop Until X = 0
The loop above will loop until X is equals to 0, the it will stop.
That was all for this time, we'll take the two last loops later on in the series.
Edited by Vswe, 21 March 2010 - 02:31 PM.