Array in QBASIC


Array is a variable which stores different values of the same data type. It is useful to organize multiple variables. To declare an array DIM (dimension) statement is used. Lets take an example to declare an array.

DIM numbers(10)

Here DIM is a keyword to declare array, similarly 'numbers' is a name of array. If you want to create a string array variable then you need to add dollar ($) at the end of the name of array variable. 10 inside the parentheses means this array can hold 11 elements, yes 11 elements there is a first index array 0 also (0 to 10), but it can store only number value because 'numbers' is a numerical variable. Lets see simple example of array in QBASIC.

It is useful to organize multiple variables. To declare an array DIM (dimension) statement is used. Lets take an example to declare an array.

Example 1:

CLS
DIM num(5)
num(0) = 2
num(1) = 4
num(2) = 5
num(3) = 10
num(4) = 6
num(5) = 9
PRINT num(0); num(1); num(2); num(3); num(4); num(5)
PRINT num(3) + num(5)
END

Output

2 4 5 10 6 9
19

Example 2:

We can take data in array from the user by using looping statement. Following program will take 5 numbers from the users and print the sum of numbers.

CLS
DIM num(4)
sum = 0
PRINT "Enter any 5 numbers"
FOR i = 0 TO 4
     INPUT " :: "; num(i)
     sum = sum + num(i)
NEXT i
PRINT "The Sum = "; sum
END

  9099