If you have a PH account, you can customize your PH profile.



Do While…Loop
The general structure of the Do While...Loop is

Do While Boolean expression
     Statement or block of statements
Loop


The statements under Do While will run continuously as long as the Boolean expression evaluates to true. The similar code for printing integers 1 to 10 on Console using the Do While...Loop is

Dim i As Integer =1
Do While i<=10
     Console.WriteLine("In the loop, value of i is " & i)
i = i + 1
Loop


Do…Loop While
A Do...Loop While is similar to a Do While...Loop, except that it does not check the condition before entering the first iteration (execution of code inside the body of loop). The general form of the a Do...Loop While is:

Do 
statement or block of statements
Loop While Boolean expression


The statements under the Do will be executed first and then the Boolean condition is checked. The loop will continue until the condition remains true. The code which prints integers 1 to 10 on console using Do...Loop While is

Dim i As Integer = 1
Do
Console.WriteLine("In the loop, value of i is " & i)
i = i + 1
Loop While i<=10


The important point is that the statements in a Do...Loop While execute at least once.

Do…Loop Until
A Do...Loop Until is similar to the Do...Loop While, except that it continues to execute the containing statements until the condition against the Until part evaluates to True or the condition against the Until remains False. The general form of the Do...Loop Until is as follows:

Do 
	statement or block of statements
Loop Until Boolean expression


The statements under the Do will execute first and then the condition is checked. The loop will continue until the condition remains false. The following code will print integers from 1 to 10 on console using the Do...Loop Until.

Dim i As Integer = 1
Do
     Console.WriteLine("In the loop, value of i is " & i)
i = i + 1
Loop Until i=10


Again the statements in Do...Loop Until execute at least once.

Arrays in VB.Net


Declaration
An Array is a collection of values of similar data type. Technically, VB.Net arrays are of reference type. Each array in VB.Net is an object and is inherited from the System.Array class. Arrays are declared as follows:

Dim <identifier>(<size of array>) As <data type>


Lets define an array of Integer type to hold 10 integers.

Dim myIntegers(9) As Integer


The above will create an array of 10 integers from the index of 0 to 9. The size of an array is fixed and must be defined before use. You can also use variables to define the size of array like so:

Dim size As Integer = 10
Dim myIntegers(10-1) As Integer


You can optionally perform declaration and initialization in separate steps like below:

Dim myIntegers() As Integer
myIntegers = New Integer() {1, 2, 3, 4, 5}


Here we initialized the array myIntegers using the values it holds. Note, we must enclose the values in curly brackets and separate the individual values with commas. This will create an array size of 5, whose successive values will be 1, 2, 3, 4, 5

It is important to note that when an array declaration and initialization are performed separately, you must provide values for each element.

Accessing the values stored in array
To access the values in an Array, we use the indexing operator (Integer index) by passing an Integer to indicate which particular index value we wish to access. It's important to note that index values in VB.Net starts from 0. So, if an array contains 5 elements, first element would be at index 0, second at index 1 and last (fifth) at index 4. The following code demonstrates how to access the 3rd element of an array

Dim myIntArray() As Integer
myIntArray = New Integer() {5, 10, 15, 20}
Dim j As Integer = myIntArray(2)


Lets make a program that uses an integral array.

' demonstrates the use of arrays in VB.Net
Public Sub Main()
' declaring and initializing an array of type integer
Dim myIntegers() As Integer = New Integer() {3, 7, 2, 14, 65} 
' iterating through the array and printing each element
Dim i As Integer 
For i = 0 to 4
Console.WriteLine(myIntegers(i))
Next
End Sub


Here we used the For...Next loop to iterate through an array and use the Console.WriteLine() method to print each individual element of the array. Note how the indexing operator () is used.

The above program is quite simple and efficient, but we have to hard-code the size of the array in the For...Next loop. As we mentioned earlier, arrays in VB.Net are reference type and are a sub-class of the System.Array Class. This class has a lot of useful properties and methods that can be applied to any instance of an array. Properties are very much like a combination of getter and setter methods in common Object Oriented languages. Properties are context sensitive; meaning the compiler can un-ambiguously identify whether it should call a getter or a setter in certain contexts. We will discuss properties in detail in the coming chapters. System.Array has a very useful read only property “Length” that can be used to find the length or the size of an array programmatically. Using the Length property, the For...Next loop from the previous code example can be written as follows

For i = 0 to myIntegers.Length - 1
    Console.WriteLine(myIntegers(i))
Next


This type of loop is very flexible and can be applied to an array of any size and of any data-type.

We can also understand the common description of the Main() Sub procedure. Main is also declared as

Public Main(ByVal args As String())


The command line arguments that we pass when executing our program are available in our programs through an array of String type, which is identified by args string array.

The For Each Loop
There is another type of loop that is very simple and useful to iterate through arrays and other collections. This is a For Each loop. The basic structure of the For Each loop is

For Each <identifier> in <array or collection>
         <statements or block of statements>
End For


Lets now make our previous code iterate through an array with a For Each loop

' demonstrates the use of arrays in VB.Net
Public Sub Main()
' declaring and initializing an array of type integer
Dim myIntegers() As Integer = New Integer() {3, 7, 2, 14, 65} 
' iterating through the array and printing each element
Dim i As Integer 
For Each i in myIntegers
    Console.WriteLine(i)
Next
End Sub


Simple and more readable! Isn't it?

For Each i in myIntegers


We declared the variable 'i' to hold individual values of array 'myIntegers' in each iteration. It is necessary to specify the type of variable 'i' same as the type of elements in the collection (Integer in our case).

Important points to note here are
  • Variables are used to hold individual elements of an array in each iteration (i in the above example) are readonly. You can't change the elements of an array through it, you can only read it. This means For Each only allows you to iterate through the array or collection and not to change the contents of it. If you wish to perform some work on array elements such as to change the individual elements, you should use the For...Next loop.
  • For Each can be used to iterate through arrays or collections. By collection, we mean any class, struct or interface that implements an IEnumerable interface. (Just go through this point and come back to it once we complete the lesson describing classes and interfaces)
  • The String class is also a collection of characters (implements IEnumerable interface and returns the Char value in the Current property). The following code example demonstrates this and prints all the characters of a string.
Public Sub Main()
Dim name As String = "Faraz Rasheed"
Dim ch As Char
For Each ch in name
    Console.WriteLine(ch)
End For
End Sub


This will print each character of the name variable on a separate line.


 

Other Views

corner
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.