Write a QBasic program using string and numeric variables.
Answers
Answer:
Variables.
A variable is just like the variables used in math class. If I showed you x + 3 = 4, then you would know that
x = 1 because subtracting 3 from each side leaves x by itself on the left while 1 is left on the right after solving 4 – 3.
Well variables in programming do the same sort of thing. They are names created by the programmer to hold a value. There are two kinds we will discuss in this lesson and they are numeric variable (hold numbers) and string variable (hold strings).
The first one we will discuss is numeric variables. Numeric variables are used just like the variables in math, so you could say something like
PRINT x + y
but instead of you solving for x and y, you get to state what they are and the interpreter does the rest for you. Let’s make that example work like a real program.
-----CODE----- CLS x = 4 y = 2 PRINT 4 + 2 PRINT x + y -------------- -----OUTPUT----- 6 6 ----------------
Notice that I assigned the variable x to 4 and y to 2 using the equals sign. So I told the interpreter to make
x = 4 and y = 2, which resulted in a 6 when I used them with the second PRINT command. The first PRINT is used to show that the addition works both ways. It is unnecessary, so feel free to ignore it for this example.
Variables do not have to be one character long, though. As long as the variable name is less than 40 characters, you can use it, but try not to go above 10 characters. It could get annoying to continually type a long variable name. If I wanted to use seven = 7, then I could do that or I could type answer = 42. It all depends on what name will have a meaning to you. Usually it is helpful to name something after what you are using it for. For example, if I wanted to do some math, then I could do the following:
answer = 4 + 2
The variable answer would then contain the answer of the math I just did. As long as you can remember it, though, the name should be fine.
A few more notes to remember about the naming of variables. The name of variable must:
Only contain numbers and/or letters, no symbols
Not be a keyword. A complete list of keywords can be seen from the following link
Keywords are the words that already have a function or use in the language. If you tried to use them, then the interpreter would be confused and wouldn’t let you run the program.
Be less than 40 characters
Some examples of good variables are:
answer
7eleven
seven11
variable6354
If you follow those 3 rules, then your variable’s names will be fine. If you do not follow those rules, then you will not be able to execute any program you write, which would be bad.
I hope you understand the use of the numeric variable. It really isn’t too bad. It is used like it is in math (there are a few differences, but you do not need to know what yet) and it can only contain numeric values.
Next on the agenda is the string variable. These kind of variables contain strings, like the name suggests, but they have a special symbol in them that distinguishes them from the numeric variable. The naming rules still apply to string variables, but at the end of string variables, you need to put a dollar sign ($). These are some examples of string variables:
str$
answer$
7eleven$
55flamingo44$
The dollar sign ($) is the way you can tell a string variable from a numeric variable. An example of string variables in a program example would be the following:
-----CODE--------------------- CLS first$ = "Gandalf" last$ = "The Grey" name$ = first$ + " " + last$ PRINT first$; " "; last$ PRINT name$ ------------------------------ ------OUTPUT----------- Gandalf The Grey Gandalf The Grey -----------------------
This may look a little confusing at first, but it isn’t too bad. I assigned “Gandalf” to first$ and “The Grey” to last$. After that assignment, I now have two string variables with values. Next I take first$, a space, and last$ to make one string out of them. Once I make that one string, I assign it to name$. If I didn’t make one string variable, then I would have to print out the entire name using the first PRINT statement, but since I did make one string variable (name$) that has the added value of 3 strings, I can print that to the screen easier by using the one string variable name$.
I hope that isn’t too confusing. I’ll give yo one more example and I hope this clears any problems up.
-----CODE--------------- CLS x$ = "4" y$ = "2" answer$ = x$ + y$ PRINT answer$;x$;y$ ------------------------ -----OUTPUT------------- 4242 ------------------------
In this example, I’ve assigned values to the first two string variables and then I add those string variables and put that value in answer$. The output is then just printing out the values of those variables.
answer$ = “42”
x$ = “4”
y$ = “2”.
That is numeric and string variables.
Hope it helps. : )