Simple Output and Variables

        Generally in a program you will need a way to tell the user something. QBasic does it with the PRINT statement. You can print plain text messages, or variables to the screen with this statement.
In the example below the phrase hello world! will be printed to the screen. In fact anything you put inside of the " " will be printed.

        PRINT "hello world!"

Output Screen:

        hello world!

Now you won't always want to print just a string of pre-defined text. You may want to print a computed value or something. We can do this with the use of variables. The main purpose of variables is to store values.
        There are different types of variables for different types of data, further discussion of data types is included in later sections and the appendix to this tutorial. It won't hurt to skip that part, but it may help to read it over quickly.
        To make a variable a particular data type, all you need to do is place the symbol of that data type at the end of the variable. As shown in examples below, the % describes the data type, which is integer.
        Variable names can be pretty much anything, except words that already have some pre-defined meaning in the language, like PRINT or CLS for example. Also variable names can contain numbers, but cannot be numbers, or start with a number, for example:     A1% is a valid integer value. but 1A% is not.
We can use Variables to store computed values, or they can be used to compute values. Example:

A1% = 1 + 2
or
A1% = A1% + 2

At the beginning of a program, your variables have the value of 0, so before you can use them you must give them a value. If you don't they will remain as 0 until you do so. Qbasic follows basic Algebrea in the way that values are computed, so any order of operations that you know will hold true here. If you want to know more see Mathematical operators and functions which is an appendix included with this tutorial.

A1% = 5

So now A1% has the value of 5, now that we have a value stored inside of the variable we can print it to the screen like this:

        PRINT A1%

Output:

        5

You can also print a sentence followed by the variable, like this:

        PRINT "The value of A1 is", A1%

Output:

        The value of A1 is                 5

Notice that 5 is spaced pretty far away from the rest of the text, this can be fixed by using a (;) instead of a (,) between the text and the variable. The comma acts like a tab, which tabs a distance of about 10 characters or so. It is legal to place more than one comma in a row to get larger tabs.
        The last thing we will discuss here is the placing the value of a variable in between two pieces of text. Example:

        A1% = 10
        PRINT "I Think that"; A1%; "is a good number

Output:
        I Think that 10 is a good number

        There is still more on the discussion of Output and variables to be covered, however I feel that it is best to get a larger programming foundation before continuing the discussion of those.

    Back         Tutorial Part 1         Next    
<-Back | Next ->