A variable refers to a position or location in the memory of the computer where a value can be stored.
Initially – dose not need a value
The word variable indicates that the value of the variable may vary as needed or processing is done.
Naming a variable
Every variable is given one exact name (Descriptive Name)
Rules for naming variables
1: Each must have a unique name
2: Each must have a descriptive name
3: Cannot contain spaces (words can be joined to form a name: bookPages or book_pages)
4: Cannot contain only numbers
5: Names must start with a letter
6: Name cannot contain special characters
7: Must be short and descriptive as possible
How to name a variable
Convention is to use only lover-case letters in the name of a variable. Except where two or more words are joined. In such cases all the following words start with upper-case which is known as camel casing.
|Name of employee||empName|
|Price of car||carPrice|
|Author of book||author|
|Quantity in stock||quantity|
|Age of student||stAge|
|Total number of sales||totNumSales|
Types of variables
The following data types are applicable when variables are classified
2.1.1 Numeric variables
Whole numbers (no fractional or decimal)
Example: 15 -2334 9728 0
Real number variable
Contains decimal point
Example: 12.47 -987.121 17.00
2.1.2 None – numeric variables
Single letter, number or special character (Always enclosed in quotes)
Example: “A” “G” “*” “8”
The character “8” which is declared as a character is not regarded as a numeric and may not be used in calculations. When declared as a character, it can be used as a code – for instance, the group number is “8”
Two or more character (must be enclosed in quotes)
Example: “32 Long Street”
Or a message, such as “The name is found”
Can only contain a true or false value.
In a computer language, a true value is represented by a no-zero value, whereas a false value is represented by a zero.
A Boolean variable is used to test if a specific condition is true or false. (Also called a logical variable)
Example: Is the person a club member? True or False
The value of a variable
A variable need not contain an initial value. A value can be assigned to a variable according to its data type.
For instance – The variable costPrice must contain a numeric real value.
A value can be assigned to a variable while the statements in the algorithm are being processed.
Rule: When the value of a variable changes the previous value of the variable is replaced by the new variable
Use same rules as variables but have:
Value cannot change
Constants are used where we know the value will never vary during the execution of the program.
(Can be any type of Data)
There is always 24 hours in a day.
4: Using variables in expressions and assignments
The assignment symbol (=) is used to assign an initial value or an expression to a variable.
|Syntax:||Variablename = Value|
|Varaiblename = Expression|
|Example:||totalNumStudents = 20|
|totalNumStudents = numMales + numFemales|
The example, totalNumStudents = 20, means that a variable that contains the total number of students is equal to 20.
The second example, however indicates that the total number of students is equal to the number of male students plus the number of female students. numMales and numFemales are both names of variables.