Make your own free website on Tripod.com
QBasic - Step By Step :: Section 1
Author: Mike Ware
Website: [warebiz] :: "The Programmer's Domain" - http://warebiz.tripod.com
Email: warebiz@yahoo.com
Copyright © 2002 Michael Shawn Ware, All Rights Reserved.


You are here --> [Section 1 :: Fundamental Aspects of the Language]

"Jump To Articles"
    --> Getting Started
    --> Data Types / Variables
    --> Dimensioning Variables
    --> Simple QBasic Statements


Getting Started

Before we start to dig into the heart of QBasic, it is necessary for you to get a brief introduction of some basic computer programming aspects. QBasic is a very simple structured programming language. Programs written in QBasic are constructed with easy-to-follow logic, attempt to use only three basic control structures, and are divided into modules. A module is a subpart within a program designed to perform a specific task (in C++, a module would be referred to as a function). QBasic is very simple to learn, and it is a great first language for any beginning programmer.

Programs are created and ran (tested) using a compiler, commonly referred to as a SmartEditor in QBasic, which interprets the code and performs tasks accordingly. A program is a sequence of instructions programmers devise that tells the computer how to solve a particular problem. A program consists of many QBasic statements, which are composed of programming commands (called keywords which have special meaning to QBasic) and other language elements which the programmer specifies.

Every program will be primarily made up of many different elements, such as variables and constants. A constant (numeric or character) is a value that does not change during execution of a program. Some valid and invalid constants are outlined below:

Valid Numeric Constants
    15.0
    .45
    -9.876
    5423
    -90

Invalid Numeric Constants
    17,974.5     --> (no commas)
    34.34%     --> (no %)
    1 345.23     --> (no spaces)

A character string constant is any collection of characters enclosed in double quotation marks. The length of the string is determined by counting each individual character in the string. The maximum length for a string in QBasic is 32,767. Some valid and invalid character string constants are outlined below:

Valid Character String Constants
    "QBasic"
    "234"
    "-.019"
    "Programming is simple with QBasic"
    "***** Hello, World *****"

Invalid Character String Constants
    "Who are "you"?"     --> (no inside double quotes)

Data Types / Variables

Variables are extremely important to understand. A variable is a value associated with a storage location in memory of a computer whose value can change during program execution; the memory location remains the same, but the variable's value may change. Variables allow the programmer to manipulate data however many times he needs to during program execution. Programmers give names to memory storage locations called variable names so they can use them in programs. It is important to note that when you name variables, you should use descriptive names so you and others will be able to understand and read the program code easily. The name of the variables should reflect the value of what the variable holds or will hold.

Data Types
There are two types of variables in QBasic: numeric and string. A numeric variable can further be broken down into three categories: integer, single precision, or double precision and is capable of storing numeric values. A string variable is capable of storing character string values.

A numeric variable name must begin with a letter, followed by letters, digits, and periods (no blanks or underscores). By default, a variable will automatically be thought to be of single precision type, which is capable of accurately displaying numbers up to seven decimal places. You can specify a variable to be of single precision type by using the following syntax:

    data
    data!

Notice that an exclamation point ( ! ) is attached to the end of the second variable name. This is not needed but is often used in programs so the programmer will know that data is of single presicion type. A double precision type is used when the programmer is working with large values that may need to display numbers up to fifteen decimal places. You can specify a variable to be of doulbe precision type by using the following syntax:

    data#

Notice the pound symbol ( # ) attached to the end of the variable. This lets QBasic associate data as being of double precision type. There may be times when you are strictly working with integer values and have no need to use floating point values (single or double types). In this case, you can specify the variable to be of type integer. You can specify a variable to be of integer type by using the following syntax:

    data%

Notice the percentage symbol ( % ) attached to the end of the variable. This lets QBasic associate data as being of integer type. If working with extremely large integral values, you can specify the variable to be of type long by using the following syntax:

    data&

Notice the ambersand symbol ( & ) attached to the end of the variable. This lets QBasic associate data as being of long integer type.

Some valid an invalid numeric variable names are provided below:

Valid Numeric Variable Names
    firstTest
    profit&
    gpa!
    total.Price
    company8
    COURSE2D

Invalid Numeric Variable Names
    1stClass     --> (must begin with letter)
    total Price     --> (no spaces)
    name,First    --> (no commas)

A string variable name must begin with a letter, followed by letters and digits, and must be terminated with a dollar sign ($). The dollar sign simply alerts the compiler that the variable is a string variable. Some valid and invalid string variable names are provided below:

Valid String Variable Names
    firstName$
    heading$
    course23Name$

Invalid String Variable Names
    firstName     --> (must end with $)
    company Name$     --> (no spaces)
    name,First$    --> (no commas)

Dimensioning Variables

QBasic allows programmers to dimension or declare a variable by using the DIM statement. If you choose to dimension variables in this way, the DIM statement(s) must be placed at the very beginning of your program before the variable(s) are used in the program. After you declare a string variable using the DIM statement, you do not need to attach the dollar sign ( $ ) to the end of the string variable thereafter in the program and QBasic will actually generate an error if you try to. The DIM statement is used to declare variables as follows:


    DIM state AS STRING
    DIM number AS INTEGER
    DIM profit AS DOUBLE
    DIM gpa AS SINGLE

When declaring string variables, you can also take advantage of specifing fixed lengths or a maximum amount of characters for the string variables. If declared as being with a fixed length, the variable will always contain that specifed length in all situations. If you try to place less characters into the string than the max size, spaces will be provided for the trailing characters. If you try to place more characters into the string than the max size, the extra character values will be truncated off. Fixed length declarations can be done as follows:


    DIM name AS STRING * 25


QBasic programs also contain many keywords, which are pre-defined in QBasic and have special meaning. Some keywords include READ, LIST, PRINT, and END, which I describe later. Because keywords are pre-defined and have special meaning, you cannot use them as variable names.

In the next section, I will introduce you to simple QBasic statements. Read on for more...

Simple QBasic Statements

The following statements are extremely simple to learn, and you will probably use them in most of your programs.

CLS - simply clears the output screen and positions the active cursor to the upper left corner of screen (0,0). You should almost always place CLS at the very beginning of your program code so you will not see any output produced from previous programs when you execute your current program.

REM - used for documentation purposes. Documentation is very important in programming and you should get into the habit of documenting your code. Documentation statements are not executed by the program and do not affect the program code. They simply tell the programmer or other programmers viewing the code what particular segments of the code are supposed to do. You should also place appropriate documentation at the beginning of your code explaining the purpose of the program, the date last updated, your name as the programmer who wrote it, and any other information that may be appropriate for the program.

' (single quotation mark) - also used for documentation purposes

LET - implies an assignment statement, which are statements used to assign a value to a variable. For example, consider:

    LET total = 90     --> assigns value of 90 to total
    LET name$ = "your name"     --> assigns value of your name to name$

NOTE: LET is optional and does not need to be included in the program code. For example, the above statements could also be written as:

    total = 90     --> assings value of 90 to total
    name$ = "your name"     --> assigns value of your name to name$

For simplicity sake, I will not include LET during assignment statements throughout the rest of this tutorial.

Obviously, if we are going to talk about assignment statements and variables, we will need to know the type of arithmetic operators that are compatible with QBasic. The basic arithmetic operators are as follows:

    + : addition
    - : subtraction
    / : division
    * : multiplication
    ^ - exponentiation

The operator precedence (order of operations) is as follows:

    1 - Exponentiation
    2 - Multiplication, Division
    3 - Addition, Subtraction
    4 - same level ops. are preformed from left-to-right

For example, consider the following statements:

    A = B ^ 3     --> B is cubed and the result is stored in A
    A = 2 * 7 + 10     --> A is assigned 24

NOTE: You must use parentheses if you want to force the compiler to evaluate particular expressions in a statement before others. For example,

    X = (A + B) * C     --> A is added to B and then that result is multiplied by C and stored in X

Ok, so we know about variables and operators, but how do we actually display results to the output screen. We use PRINT to output information to the output screen. I explain further in detail about the PRINT statement and output in the next section.

PRINT - prints literals, values of variables, and arithmetic expressions to output screen

You can use commas to format a PRINT statement which insert a tab space across the output line. For example,


     PRINT "NAME", "TEST", "SCORE"

would output something like:

     NAME     TEST     SCORE

When using the PRINT statement, all character values must be enclosed with double quotation marks, but numeric literals do not need to be enclosed with double quotation marks. Also, issuing PRINT, alone, prints a blank line on the screen (skipping a line by leaving it blank).

END is a statement that should be placed at the end of every program you write. END instructs the compiler to terminate program execution.

The following is a program with basic statements (which are described above) that you can save and use as a default setup program for when you start to write a new program:


    REM PURPOSE:
    REM
    REM
    REM PROGRAMMER:
    REM DATE LAST UPDATED:

    CLS


    END

We can now start to talk about how you can interact with the user of a program by using INPUT/OUTPUT statements. Read on for more...

Move on to next set of topics: Section 2 - Input and Output

Back to Top