When a computer asks for something from the user (input data), it stores it temporarily in the computer's memory. A VARIABLE is defined as a temporary storage in the computer's memory. It is like a box or a container where you put the data (letters and/ or number) inside.
Types of Variables
There are two basic types of variables.
1. Numeric variable - a box in the computer's memory that can only contain numbers used in calculation.
2. String variable - a box in the computer's memory that can contain a group of letters or words or combination of numbers and words or even a complete sentence; the number inside this variable cannot be used in calculations.
Say you have several boxes in your room. What will happen if your boxes have no label? You might misplace your files or your stuff, right? Or what if they have labels but are represented by numbers from 1 to 100 or A to Z without any specific reason?
Or what if you were looking for a pencil inside the container labeled "pencil" and finds out that this container actually contains crayons.
It is very important that we name our variables ("containers") properly. You can label your containers or in computer terms name your variable with any name that you want.
We do need to follow some rules in naming our variables. These rules are also known as proper syntax names of variables.
Syntax for NUMERIC Variables
Rules to Follow when Using a Numeric Variable
RULE 1: You may use any name for your variable. Althougn it is always better to choose a name that would describe its numeric content.
RULE 2: You may combine the name with a number as long as the name doesn't start with a number (Correct: num 1 or num2 ; Wrong: 1 num).
RULE 3: NEVER use spaces when naming your variables. These might confuse the computer. It might think that you are using two variables already. (for example, if you use favorite number, it could mean that the first variable is named favorite and the second variable is named number; use favoritenumber instead)
RULE 4: Not CASE SENSITIVE - it means that you may use a capital or a small letter for your variable names (example: favoriteNumber is equal to Favoritenumber is equal to FavoriteNumber etc..)
RULE 5: The content of the variable can range from -32,768 to +32,767 characters only (or elese the computer will reply "Overflow").
RULE 6: NEVER use a name of a COMMAND or FUNCTION that are used already by Qbasic. (example: Don't use the name PRINT because this is a command).
NUMERIC VARIABLES WITH COMPUTATIONS
Here's an example of using a numric variable with some computations.
PRINT "Let's get the average of 4 subjects"
PRINT "Math = ", math
PRINT "Science = ", sci
PRINT "English =", eng
PRINT "History =", hist
average = (math + sci + eng + hist) / 4
PRINT "The average is = "; average
The output screen of the code above would be:
Let's get the average of 4 subjects
Math = 98
Science = 95
English = 96
History = 93
The average is = 95.5
Instead of using a semicolon to separate the words to be printed and the contents of a variable, a comma can also be used. Recall the purpose of a comma in displaying data in print zones.
Syntax for STRING Variables
Rules to follow When Using a String Variable
RULE 1: You may use any name for your variable as long as this name describes its string content.
RULE 2: The rules in using the numeric variable are the same, except that you need to add a $ dollar sign at the very end of the name to signify that you are using a string variable.
RULE 3: The contents should always be placed inside quotation marks (" "). (Example: nickname$ = "mae", or lastname$ = "punzalan")
RULE 4: The contents of the string variable may range from 0 to 32,767 characters
RULE 5: NEVER use a name of a COMMAND or FUNCTION that are used already by Qbasic. (Example: Don's use the name PRINT because this a command.
Concat-uh-what?! It's just a fancy word for putting things together, one after another. It's much easier done than said. When you use the plus-sign "+" with strings, it doesn't add them up. Instead, it puts them together.
A$ = "Hello "
B$ = "there!"
C$ = A$ + B$
That will print "Hello there!" to the screen.
LEFT$() and RIGHT$()
LEFT$() and RIGHT$() let you work with parts of strings. Try this example:
A$ = "Ted Felix"
B$ = LEFT$(A$, 3)
LEFT$(A$, 3) means "take 3 characters from the left of A$". Since the 3 characters on the left happen to be "Ted", this program prints "Ted" as expected. Try changing the number to 2 or 5 and see what happens.
Once you understand LEFT$(), RIGHT$() is easy. Let's try it:
A$ = "QBASIC is cool"
B$ = RIGHT$(A$, 4)
RIGHT$(A$, 4) means "take 4 characters from the right of A$". This gives us "cool".
LEFT$() gives us something from the left side of a string. RIGHT$() gives us something from the right side of the string. MID$() gives us something from the middle of a string. Try this:
A$ = "one two three"
B$ = MID$(A$, 5, 3)
MID$(A$, 5, 3) means "take 3 characters from the middle of A$, starting at the fifth character". This gives us the word in the middle: "two".
You can also use MID$() to change a portion of what is in a string variable. Try this:
A$ = "cabinet"
MID$(A$, 4, 2) = "ar"
Here, we replaced the "in" in cabinet with "ar". This gives us a completely different word.
This would be a pretty sneaky way to hide something like a password in a program. Someone who didn't know how to program in QBASIC might not be able to figure it out.
LCASE$() and UCASE$()
If you need to convert a string to all uppercase or all lowercase, UCASE$() and LCASE$() are exactly what you need.
A$ = "Fly Away With Me"
You can use UCASE$() and LCASE$() to do "case-insensitive" tests. In other words, upper and lower case are ignored. Here's an improvement to a previous program.
INPUT "Enter your name: ", Name$
IF LCASE$(Name$) = "mike" THEN
PRINT "Go Away!"
PRINT "Hello, "; Name$; ". How are you today?"
In this new version, the user can type "mike", "Mike" or even "MIKE" and the name will be recognized.
STRING$() and SPACE$()
Let's say you need to print 20 stars on the screen in a line. You could do it like this:
But, there has got to be a better way. How about with a FOR loop?
FOR I = 1 to 20
That works well, but QBASIC provides an even easier way to do this with STRING$().
PRINT STRING$(20, "*")
The first argument to STRING$() is the number of times you want a character repeated. The second argument is the character you want to repeat. So, STRING$(20, "*") means "give me 20 stars".
If you want to print a lot of spaces, you could do it with STRING$():
PRINT "A"; STRING$(20, " "); "B"
Or you can use SPACE$().
PRINT "A"; SPACE$(20); "B"
FOR loops always make things interesting:
FOR I = 0 to 20
PRINT SPACE$(I); "QBASIC!"
LEN() gives you the length of a string. Try this:
A$ = "Hello"
As expected, that should print the number 5 since the word "Hello" has 5 characters.
LEN() is handy in FOR loops when you aren't sure how long the string is, and you don't feel like counting it yourself:
A$ = "Hello QBASIC!"
FOR I = 1 to LEN(A$)
PRINT LEFT$(A$, I)
LEN() is very useful when you want the user to provide the string. Try this:
INPUT "Enter a string: ", A$
PRINT "The string you entered was"; LEN(A$); "characters long."