Professional Documents
Culture Documents
OBJECTIVES
Recall the array declaration
Demonstrate types of arrays
Explain the for loop procedure used in arrays
Syntax for declaring an array in Visual Basic
Dim GreekLetter(4) As String
1. Array Declaration:
Pseudocode: Set num[1:5] OR num[1:5]
VB : Dim num(5) as integer
Multidimensional Arrays
Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent tables of values
consisting of information arranged in rows and columns. To identify a particular table element, we must specify two
indexes: The first (by convention) identifies the element's row and the second (by convention) identifies the
element's column. Note that multidimensional arrays can have more than two dimensions
Tables or arrays that require two indexes to identify a particular element are called two dimensional arrays. . Visual
Basic supports at least 60 array dimensions, but most people will need to use more than two or three dimensional-
arrays.
The following statement declares a two-dimensional array 50 by 50 array within a procedure.
Dim twoDarray(10,
Dim AvgMarks ( 50, 20) 50
As )
Integer 'a two dimensional array of integers Dim ranges(10,
100) 'a two dimensional array
Program to input an array value at a given index
Public Class Form1
Dim Task(3) As Integer
End Sub
For Loop
✖ Repeats a selection of code predetermined number of times. When the number of repetition
is known or can be calculated.
✖ These are also known as ‘Count-Controlled’ loops because the number of iteration is
controlled by a loop counter. Commonly variable ‘i’ is used for the counter variable.
✖ A code is placed within the For loop which will be repeated.
✖ Syntax:
For i = 1 To 10
‘ Code to execute’
Next
✖ The loop counter may have reached the final value, but Next will increase it to next number
before passing to For. The value will be outside of the criteria so the program will then exit
loop.
NOTE: if the array is declared as num[1:10], that means it has indexes from 0 till 10.
Then: FOR – TO – NEXT LOOP must be from 1 TO 10
it should be used as:
FOR a = 0 TO 9 if we use
Input num(1)
Input num(2)
Input num(3)
Input num(4)
Input num(5)
Input num(6)
Input num(7)
Input num(8)
Input num(9)
Input num(10)
We may alternatively use:
num[1:10]
FOR a = 1 to 10
Input num(a)
NEXT
This techniques is even convenient for multiple variables of same attribute.
Array is used in two ways:
1. Declaration
2. Manipulation (Initialization/input/calculations/output)
1. Array Declaration:
2. Use/Manipulation:
FOR a = 1 to 5
Input num(a)
NEXT