Home Previous section Next section

Variable types Star


The non-string variables we've used in this tutorial are actually called single-precision variables. These types of variables (SINGLE's) are used to store numbers that can contain a decimal value (such as 1.89 or 3.141593). Since they have decimal values, they are also known as "floating-point" variables.

This chapter describes other types of variables used in QBasic.

    INTEGER - A non-floating-point variable (no decimal value) that can store integers between -32,768 and 32,767

    LONG - Same as INTEGER, but can contain numbers between -2,147,483,648 and 2,147,483,647.

    DOUBLE - Same as SINGLE, but can have twice as many digits.


To define a variable's type, use DIM with the AS attribute.

    DIM var1 AS INTEGER
    DIM var2 AS LONG
    DIM var3 AS DOUBLE

    var1 = 15.28
    var2 = -2000000000
    var3 = 12345678.12345678

    PRINT var1
    PRINT var2
    PRINT var3
Output:
     15  (Notice how the decimal value is removed)
    -2000000000
     12345678.12345678

Using special characters

You can use special characters to specify a variable's type. These characters can also be used to specify a number's type.

To do so, place one of the following at the end of a variable (or number):
    !  (single--actually, this doesn't change anything)

    %  (integer)

    &  (long)

    #  (double)

    $  (string--as we already know)


Example (notice the number sign on 12345678.12345678):
    var1% = 15.28
    var2& = -2000000000
    var3# = 12345678.12345678#

    PRINT var1%
    PRINT var2&
    PRINT var3#
Output:
     15
    -2000000000
     12345678.12345678

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