QBasic Information Center Tutorials - Variables
These tutorials were written to help you get a quick, but thorough, understanding of QBasic -
the scope of the language as well as it's specific capabilities.
Variables
Like all languages, QBasic uses variables to store values. In QBasic all
variables begin with a letter (A-Z). The variable names can also contain
the number 0-9 or a period. Variable names can be up to 40 characters long.
Creating a Variable
Unlike some languages which require that you define a variable before
using it, QBasic allows you to define a variable by simply using it in
a line of code.
a$ = 5 # a$ is created simply by assigning it a value
When a variable has not yet been assigned a value, QBasic sets the value to 0
for numbers or to "" for strings.
Variable Declaration
Instead of creating a variable by simply using it, a variable can also
be declared using one of the following methods. Discussion of how to
use these is found in the Variable Declaration
page.
|
| dim, redim, const
|
|
| defint, defdbl, defsng, defstr, deflng
|
|
| static, shared
|
|
| type
|
Initial Values
QBasic will assign numbers an initial, default value of zero and strings a default
value of "" (empty string).
Case Sensitivity
QBasic is not case-sensitive. The variables dog$ and DOG$ refer
to the same variable.
Note: the QBasic IDE will detect variations on capitalization of variable
names and modify the source code so that all variables of the same name are
capitalized the same way. The first instance of the spelling is used for the
template to changes all subsequent instances of the variable name.
Good Practice
Using variable names which describe the content of the variable is
considered good practice. For example, size$ is preferred over s$.
Programmers often use the following naming practice for variables,
which involves using more than one word in the variable name.
LastIndex # capitalize first letter of words within a variable name
|