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
Progressing further on we'll come to the two last types of loops: The For loop and the For Each loop. The reason I didn't wrote about them before was that I wanted to teach you about the arrays first.
The For loop will loop a certain amount of times and when it have looped so many times it will stop. When declaring a For loop you give it a name just like you do when you declares a variable. The For loops value(the number it's currently is on) can then be accessed by using that name exactly as you do with variables. This is the syntax for declaring a For loop:
For <loop_name> As <type> = <start> To <end> [Step <step>] 'code inside the loop Next
<loop_name> is the name of the loop. You often use "i" as the name if you don't want to name it anything special.
<type> is the type the loop should be declared as.
<start> is at what value the loop should start at.
<end> is at what value the loop should end at.
[Step <step>] is optional. <step> is how much the value should change for each turn in the loop. The default value is 1.
Two examples on how to using the For loop, one is using the default value for the step9 the other one is not:
For i As Integer = 0 To 2 MessageBox.Show(i) Next For i As Integer = 1 To 10 Step 3 MessageBox.Show(i) Next
In the first loop it will go from 0 to 2 with 1 as the step. This will result in an output of 0, 1, 2 and 3. The second one will go from 1 to 10 with a step of 3. Its output will be: 1, 4, 7 and 10.
For Each loop:
The For Each loop is a sort of For loop but its value will be the value of the element in an array at the current index, therefor the For Each loop is good to use to go through all elements in an array. Here comes the syntax:
For Each <loop_name> In <array>
'code inside the loop
<loop_name> is the name of the loop. It's this you will use to access the current element form the array.
<array> is the name of the array you want to go through.
Here comes an example on how to use the For Each loop to get all the elements in an array:
Dim anExample(2) As Integer anExample(0) = 61 anExample(1) = 15 anExample(2) = 87 For Each element In anExample MessageBox.Show(element) Next
In the above example the value of "element" will be 61 the first time, 15 the second and 87 the third since this is the three values we have stored in the array.
The above example can also be created with a For loop. It's better to use the For Each loop for it but I will now show you how it's done with the For loop so you can get a better idea how the For Each loop works.
Dim anExample(2) As Integer anExample(0) = 61 anExample(1) = 15 anExample(2) = 87 For i As Integer = 0 To anExample.GetUpperBound(0) Dim element As Object = anExample(i) MessageBox.Show(Element) Next
Now you have learned about all the different types of loops. We'll continue in the learning progress soon. Cya.
Edited by Vswe, 21 March 2010 - 02:32 PM.