Input and Output

All programs are based on inputs and outputs. In fact, much of life can be boiled down to inputs and outputs. If you put two quarters in a pop machine, you get a pop. If you send in a paper to an instructor, you expect to get it back with a grade. If you put gas into your car, you expect it to run (at least, that is how my car runs). The same goes with computers. If you put in something into the computer, you should get something out.

The input is obviously first. To input variables, you use the INPUT statement (duh!). For instance, you have this example:


INPUT number

INPUT is the command word, while number is known as the variable. In algebra, variables could represent a lot of things (usually the variable was x,y, and z). In this case, variables can be up to forty characters long, and can represent both numbers and words (as we will see later).

At the commmand above, the computer would have something like this:


?

The question mark means, "I am waiting; type something in". What is typed in, however, is a myster to both you and I.

Assuming that it meant anything to us, we would type in the number 97 (not a word. Words are different variables altogether). Number would then represent 97.

The next command that we should learn is the PRINT command. PRINT does what it says, in a way. It prints what follows it to the screen. For instance, the command:


PRINT "This is a waste of time"

Would print out:

This is a waste of time. As you may guess, PRINT can also be used with variables. Taking the example that we just typed in, let's assume that the next command in our program was:

PRINT number

Would print out:

97

Literal statements ("This is a waste of time") and variables (number or 97) can be mixed in both PRINT and INPUT statements. For instance, let's say we wanted to print out "The number is" and the number, which is a variable, we would do the following:


CLS              'Clears the screen
INPUT number     'asks for the number
PRINT "The number is"; number
END              'Ends the program

The single quote marks ' tell the program to ignore whatever follows it. What you would see in this program would be the following:

? 97
The number is 97



Press any key to continue. . .

Literals and variables can also be used in INPUT statments. This has an advantage in that we don't have to guess what the question mark means. Taking our mini-program, we could sub in the following for our previous input line


INPUT "What is the number"; number

We would then see this:


What is the number?

I didn't have to type in the question mark: QBASIC did that automatically. To get rid of the question mark, I put a space between the last character in the literal and the quotation mark.

So, our final program would look like this:


CLS
INPUT "What is the number"; number
PRINT "The number is"; number
END

A final few words about variables: as you may have guessed, variables can be added, subtracted, multiplied, and divided. The result is stored in a new variable.