Tuesday, 20 September 2016

Variables

                                               Variables
When you think, you keep words or numbers in your mind. This allows you to speak and to make calculations.

QBasic also needs to keep words or numbers in its memory. To do this, you use variables, pieces of QBasic memory, which can keep information. A variable can be named with any letter, 
for example – 

a. It can also have a longer name, which can be almost any word. It is important to know that there are two main types of variables – that keep a number and that keep a word or a string of words.

• Numeric variables. It’s basically variables named with just a letter or a word. You tell this variable to keep a number like this:

a = 15

In other words, you assigned the value 15 to the variable a.
QBasic will now know that the variable named a keeps the number 15. Now, if you type

PRINT a  

and run the program, the computer will show this number on the screen.

• String variables can keep so called “strings”, which is basically any text or symbols (like % or £), which you put in the quotes “ ”. You can also put numbers in a string variable, but again, you must include them in quotes, and QBasic will think that those numbers are just a part of text. The string variables look like this – a$. The $ sign tells QBasic that this variable contains text.

Example:

a$ = “It is nice to see you”
PRINT a$

On the screen you’ll see:
It is nice to see you

The PRINT command can print more that one string on the line. To do this, put the ; sign between the variables.

For example, you have two variables – name$, which contains name Rob, and age, which contains the number 34. Then, to print both name and age, you type:

PRINT “Name - ”; name$; “. Age - ”; age

NOTE - As you can see, the name of a variable can be more than just one letter – it can be a short word which describes what sort of information does this variable keep.

What you see on the screen when you run the program will look like this:

Name – Rob. Age – 34

Or, you can type the program like that:

PRINT “Name - ”; name$
PRINT “Age - ”; age

The result is:

Name – Rob
Age - 34

No comments:
Write comments