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.

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

Video Tutorials for more info


WE can also create an array of string variables.
Example 3:

CLS
DIM name$(3)
name$(0) = "Ramesh"
name$(1) = "Ram"
name$(2) = "Bimal"
name$(3) = "Sunil"
PRINT name$(0), name$(3)
END

Output:

Ramesh        Sunil

 

Example 4:
Following program gets the name and marks of 5 students and print them with result, by assume pass mark = 32.

CLS
DIM name$(1 TO 5)
DIM marks%(1 TO 5)
FOR i = 1 TO 5
     PRINT i
     INPUT "Enter Name : "; name$(i)
     INPUT "Enter Marks : "; marks%(i)
NEXT i
PRINT "Name", "Marks", "Result"
FOR j = 1 TO 5
     IF marks%(j) >= 32 THEN
         r$ = "Pass"
     ELSE
         r$ = "Fail"
     END IF
     PRINT name$(j), marks%(j), r$
NEXT j
END

In the above program two array variables are created first one is ‘name$’ which is string array variable and second one is ‘marks%’ numeric array variable. In the first Looping statement it will take name and marks of 5 students, and in the second loop it compare the marks with 32, if the marks is greater then or equal to 32 then it will store “Pass” value in variable r$, otherwise it will store “Fail” in variable r$ and it displays name, marks and result on the screen. Both  loop will be repeat 5 times.

Variable Types

The non-string variables we’ve used in the above program are actually called single precision variable, which is the method of variable declaration. This type of variables are used to store number that can contain a decimal value (such as 1.89, 3.1415 etc). So, they are also known as ‘floating point’ variable.

Other types of variables used in QBSIC are given below.

INTEGER : A non floating point variable (no decimal point value) that can store integers between -32768 to 32,767.

LONG : Same as INTEGER, but it contain numbers between -2,147483,684 to 2,147,483,647.

DOUBLE : Same as SINGLE but it can store twice a many digits.

Using special characters

We can use special characters to declare different types of variables. Which are given below.

! Single precision
# Double precision
% Integer
& Long
$ String

We can add above special characters at the end of the variable name to declare different variables as our required.

<< Table of Content >>