Home Previous section Next section

Arrays Star


An array is a list of variables of the same type. Arrays are useful for organizing multiple variables. To create an array, use the DIM (dimension) command.

The following example does not use arrays:
    a = 2
    b = 4
    c = 6
    d = 8
    e = 10

    PRINT a, b, c, d, e
Output:
    2         4         6         8         10

This uses an array called vars, which contains 5 variables:
    DIM vars(5)

    ' Each of these are separate variables:
    vars(1) = 2
    vars(2) = 4
    vars(3) = 6
    vars(4) = 8
    vars(5) = 10

    PRINT vars(1), vars(2), vars(3), vars(4), vars(5)
Output:
    2         4         6         8         10


How an array of variables is stored in memory

How an array of variables is stored in memory
(NOTE: Memory addresses are not necessarily as specified)



The above program can also be written like this:
    DIM vars(5)

    FOR x = 1 to 5
      vars(x) = x * 2
    NEXT

    FOR x = 1 to 5
      PRINT vars(x),
    NEXT
Output:
    2         4         6         8         10

Strings

You can also create an array of string variables.
    DIM vars$(5)

    vars$(1) = "Two"
    vars$(2) = "Four"
    vars$(3) = "Six"
    vars$(4) = "Eight"
    vars$(5) = "Ten"

    PRINT vars$(1), vars$(2), vars$(3), vars$(4), vars$(5)
Output:
    Two       Four      Six       Eight     Ten

Next section


Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc.

http://www.geocities.com/progsharehouse/qbtutor
Mirror: http://development.freeservers.com/qbtutor