Welcome! In this chapter, we will cover the following commands:
Let's get started. In the previous chapter, you learned how to use the PRINT and CLS commands. In this chapter, you will learn about variables and the INPUT command.
What are variables? Variables are "boxes" in the computer's memory to store a value - be it a number, name, decimal, dollar amount, or what have you. There are two main types of variables - numbers and "strings," which are text variables. Variables are given their type by a symbol following their name.
The "numbers" category is further broken down into four areas. The regular type, called integers, require no symbol, or can be followed by a %. They can be in the range of -32767 to 32767. Integers are what you will be using most of the time. Another type of integer, long integers, have a range of -2 billion to 2 billion. You might ask, "Why not make all numbers long integers?" There is a simple answer. The memory of the computer, especially in QBasic, is limited - you should save as much space as possible. Only use long integers where they are necessary. Long integers are noted with an &.
The third and fourth types of numbers are "floating-point" numbers. These are decimal variables that can have very long decimal spaces. Again, there are both the short types (noted by the !) and long types (noted by a #). These variables are usually not used unless you are doing specific accounting functions.
You still may be in the dark as to how variables are used. Variables are assigned a value using the LET command. For example:
LET number = 123
shortcut: You don't need the LET in front of the variable assignment
This would assign a value of 123 to the short integer variable "number." You can use math functions while assigning variables, too. "LET number = 4 * 12" would make "number" equal 48. You can increment variables like this:
LET number = number + 1
Or, you can make it "number = number + 2, number = number - 1," and so on. You can also add two variables together using the same syntax, or sequence of commands.
So now you know how to assign a value to a variable using the LET command. You need a way to output these variables to the screen if you want them to have any meaning to the user of the program. You can use the PRINT command for this task easily:
This would output to the screen the value assigned to "number." If you want to include text before the number, you must use this format:
LET number = 100
PRINT "The number is"; number
This would output, when run:
The number is 100
Pretty simple stuff. You might now be asking, "How do I ask the user for something?" You do this by using the INPUT command. INPUT basically uses the same format as PRINT. As an example:
PRINT "The number is"; number
This would give the user a prompt for a short integer to assign to "number" and then print it out. You can also supply a prompt for INPUT to use - like this:
INPUT "What is your name"; username$
PRINT "Hello, "; username$; "."
This would ask the user for a string and then output it with the PRINT command.
In this chapter, we've gone into a bit more depth with the PRINT command and learned about the LET and INPUT commands. Please wait until Chapter III is done - expected date: 6/10/96.
1. Fool around with both the LET and INPUT commands.
2. Write a program to input the user's name and age and output them using the PRINT command.