STORING VALUES IN THE COMPUTER’S MEMORY

A variable is a named storage place in memory.

There are 2 kinds of memory:

  • Temporary memory (RAM), holds the work your computer is currently processing.
  • Permanent memory (disk memory), holds data after you switch off your computer.

A variable is stored in temporary memory.
Therefore, it only exists when you are running your program.

The computer can only process data after the data is stored in memory.

Defining Variables

Disk files have filenames similarly variables have variable names.  You, the programmer, must supply the name when you define the variable.  When naming a variable you must follow these rules:

  • A variable name can range from 1 to 40 characters.
  • A variable name must begin with a letter.
  • After the first letter, your variable name can contain letters, numbers, and the underscore character.
  • A variable name cannot have any blank spaces.
  • You cannot give a variable the same name as a reserved word from the language you are using.
  • All variables within the same program must be unique.
  • A variable name must represent the data it holds.
Examples of good variable names:

Total

Sales94

Feb2001Balance

Gross_Sales

Examples of bad variable names:

#Total

94Sales

Feb-1994-Balance

Gross Sales

To create variables in QBasic or Visual Basic, you need to declare those variables using the following statement:

Dim variableName as datatype

Examples:           Dim studentName as String           Dim acct_balance as single

Data Types

Before you declare a variable you have to determine the data type of the variable.  Each variable (each tiny box in memory) must be assigned a data type by the programmer.  The data type determines the type of data the variable (memory location) can store.
The following table describes the basic data types used in QBasic

Data Type Sample values Description Memory required
Integer 219

-3333

Whole numbers – no decimals 2 bytes
Single 0.33, 4.25

-88.7

Numbers that contain decimal  points 4 bytes
Double 50000.00 Like single, but larger than 32,767 or smaller than -32,767 8 bytes
String Toronto

824-1790

6700 Edenwood Dr

Any non-numeric text, including letters, digits not used in calculations

(i.e. phone numbers, address), and special characters, in any combination

1 byte per character

Declaring a variable only creates an empty space in memory.
You will need to place some data into the variable.

Storing Data in a Variable

To place data into a variable we use either 1) an assignment statement.

or   2) an INPUT statement

( Input Box, in Visual Basic )
An assignment statement assigns a value to a variable.  The assignment symbol is ‘=’.
The variable that is being assigned a value is always on the left-hand-side of the “=”

Example 1: strStudentName=”Jill”

The previously declared variable, studentName, now holds the data “Jill”.

Note: When you store a value in a string variable, you must put the value inside quotation marks.  The quotation marks are not stored as part of the variable.

Example 2: acct_balance = acct_balance + deposit

The variable acct_balance now holds the result of the calculation on the right side of the “=”

INPUT statement –  This statement is used to allow the user to enter data during the execution of the program.  When the INPUT statement is executed, the computer will pause and allow the user to type the data; waiting until the <ENTER> key is pressed before continuing.
All input statements require an input prompt to tell the user what data is expected.

INPUT “Enter your name”, nam

OR

PRINT “Enter your name”;     semi-colon at end of first line

INPUT “”, nam

Note:  you can use a comma or semi-colon after the input prompt – a semi colon will automatically place a ‘?’ at the end of the prompt when the program is run.

Thus, there are two steps to storing data in memory:

Step1: Declare a variable                            Step 2: Assign a value

A variable holds only one piece of information.  It will hold that piece of data until you assign something else in it, or until the program ends.

You will learn later about arrays (indexed variables) that can store more than one value under the same variable name.

author avatar
William Anderson (Schoolworkhelper Editorial Team)
William completed his Bachelor of Science and Master of Arts in 2013. He current serves as a lecturer, tutor and freelance writer. In his spare time, he enjoys reading, walking his dog and parasailing. Article last reviewed: 2022 | St. Rosemary Institution © 2010-2024 | Creative Commons 4.0

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment