![]() |
VB.Net Tutorial #4
Welcome to the fourth C# tutorial in which we'll be going loopy. Loops are quite an important construct (you won't get very far in programming without them) but they can be a little difficult to get your head around sometimes. So on to our first loop which is known as the for loop:-
Imports System
Class hello
Public Shared Sub Main()
Dim i As Integer
for i = 1 to 5
Console.WriteLine("The value of i is " & i)
next i
End Sub
End Class
So what is this doing? The for statement breaks down into something like this:-
start loop
set variable to 1
if not equal to 5
add 1 to variable
go back to if statement above
otherwise
stop looping
which in VB.NET terms is:-
for variable is 1 to 5
write value to see whats happening
loop around again
It can be difficult sometimes to really understand what is happening here so take some time to think about it. You'll also need to compile and watch the program running to gain further insight. You can also play around with the start value of i (start it at zero for example) and use i<=5 and see what effect that has. Once you think you've got your head around it then move on to the next:-
Imports System
Class hello
Public Shared Sub Main()
Dim i As Integer
i = 1
Do
Console.WriteLine("The value of i is " & i)
i = i + 1
Loop While i < 5
End Sub
End Class
This is the do loop (sometimes also known as a do..while) which gives the programmer a little more control over how and when the loop variable is updated. Note that the index i variable is set before the loop starts. We also have a choice over which side of the writeline statement we add 1 to the loop variable. The while does the test for deciding when to stop the loop. Sometimes it is possible to get into a situation when the loop never stops and the it becomes difficult to shut it down so some care has to be exercised. This essentially works the same as the for loop did above just in a slightly different way. Only experience will help you to decide which loop to use in a real situation. Now something very similar again:-
Imports System
Class hello
Public Shared Sub Main()
Dim i As Integer
i = 1
While i < 5
Console.WriteLine("The value of i is " & i)
i = i + 1
End While
End Sub
End Class
This is a while loop and does away with the do. Again, you have to be careful about the start value and when it is updated inside the loop. Again, this does much the same as both previous loops but again in a slightly different way. You decide which to use. It is my aim to introduce only what you need to know in order to progress towards our ultimate aim of databases and GUI programs so you will meet these again and hopefully a few more examples will help you get to grips with it. I know that loops was one of the most difficult concepts I had to face when beginning programming but it does make sense in the end.
There is one other loop but it is involved with something called an array. An array can be though of as a series of boxes holding values. So we could for example have 3 boxes each containing an integer value as follows:-
box 1 contains the number 1
box 2 contains the number 3
box 3 contains the number 2
These are of course memory locations but don't worry so much about that as the concept. So we need to get at each of these boxes either to fill with data or update it or retrieve it. Why use arrays? Because it is more convenient to do it in this way than have a whole list of varaibles. For example, if we asigned our values using standard variables then we would need to write code like this:-
int1 = 1
int2 = 2
int3 = 3
writeline int1
writeline int2
writeline int3
Now this may not seem too bad but what if you had 1000 items. If we could use a loop then all we would have to adjust is the number of boxes - the code would stay the same. So instead we would:-
for i=1; i<1000; i++
fetch contents of array i
writeline it
loop around again
Much better. In VB.Net we use a loop that has been designed for arrays and is called the foreach loop. In pseudo code (where we write what is happening in english rather than it code as above) it would look like this:-
foreach j in the array
writeline the contents
continue looping until no more values
Note that the program doesn't need you the programmer to tell it when there are no more array values - it already knows which is very handy. So take a look at the next program:-
Imports System
Public Class ArrayExample
Public Shared Sub Main()
Dim aryInt As Integer() = {1, 3, 2}
For Each j As Integer In aryInt
Console.Write(j)
Next
Array.Sort(aryInt)
Console.WriteLine
For Each k As Integer In aryInt
Console.Write(k)
Next
End Sub
End Class
The first block of code is doing what the pseudo code above is doing. The second block will sort the array - again you don't need to write all the code to do a sort - it is available for you to use with the command Array.sort with the name of your array in the brackets. Now you can see perhaps why I used the array values of 1,3,2 instead of 1,2,3. The array is declared as integer() with the round brackets denoting that we wish to use an array of integers. This array is then assigned values. We run around a foreach loop pulling out the values and writing them to the console. Then we sort the array and print it all out again in the new order. Very nice elegant code.
So that just about wraps it up for loops. Now it is possible to nest loops within loops but I won't burden you with that unless it becomes absolutely necessary. As always, if you have any problems or queries then please email me at the address below. On to some slightly more exotic stuff next time...