Lesson 2
Lesson 2
Objectives
Introduction
The computer uses constants and variables to solve problems. They are
data used in processing.
Variables
♦ A named memory location in the computer that given a value that can be
changed any time during the processing of the program.
♦ Computer sets up a specific memory location to hold the value of each variable
name that found in a program.
♦ Variables can be any data type (few types of data)
♦ For any variables use in a program, should assign a name (called variable name
– user defined) and MUST define a data type (the type of data this variable can
hold).
♦ For example,
• value of age (variable name: age, data type – integer)
• value of cash price (variable name: price, data type – real)
• value of city code (variable name: city_code, data type – string).
Constants
♦ It is a value that is a specific alphabetical and/or numeric value that are never
changes during the processing of all the instructions in a solution.
♦ Constant can be any type of data – numeric, alphabetical or special symbols.
♦ The constant is a given location in memory and a name.
♦ For any constants use in a program, should assign a name (called constant name
– user defined) and MUST define a data type (the type of data this variable can
hold).
♦ During the execution of the program, this constant is given a value and then is
referred by its name and the value is never changed during the execution of the
program.
♦ For example, to calculate payroll for a company, constant can be the name of the
company.
Extra Rules:
• Variable/constant must be named according to what they represent (a meaningful
variable/constant), i.e. HOURS for hours worked.
• Be consistent when using upper- and lowercase characters. In some languages
HOURS is a different variable name than Hours.
• Variable name that used to represent a data item should be used consistently
throughout the program where the data item is used. For example, you may not
use HRS or HOURSWORKED to represent the same data item.
♦ The kind of data of a variable or a constant. The three basic data types are
numerical, character and logical.
Examples:
For example,
Examples:
Examples:
♦ The data that define the value of a variable or constant will most commonly be
one of three data types.
♦ The programmer designated the data type during the programming process. The
computer then associates the variable name with the designated data type.
♦ Data type cannot be mixed. For an example string data cannot be placed in a
variable memory location that has been designated as numerical or vice versa.
o The numerical data uses the set of all base 10 numbers, the plus (+) sign, and
the negative sign (-);
o The character data type uses the set of all characters and;
o The logical data type uses the set of data consisting of the words TRUE and
FALSE. The use of any data outside the data set results in an error.
♦ Any numeric item that must be used in calculations resulting in a numeric result
must be designated as numerical data type. All other numbers should be
designated as character or character-string data type.
Operators
• Operators are used to process data and to tell the computer how to process data.
• Operators are the data connectors within expression and equations.
• Three types of operators are used in calculation and problem solving include:
3. Logical, (Refer to text book, page no.22 & 23, Table 2.5 & 2.6)
AND, OR, and NOT.
• Use to join more than TWO conditions together, e.g. (8<=10) AND
(4 >10)
• The operand and resultant are two concepts related to the operators.
• Operand is the data that the operator connects and process.
• Resultant is the answer that results after the operation is completed.
For example, 5 + 7 = 12
Examples:
A = 10 B =1
Operation Resultant
A+B 11
In computer form
X is less than Y + 5 X<Y+5
X=5 Y=4
Operation Resultant
Y + 5 (4+5) 9
X < Y (5<9) TRUE
Example 1: Student must have student card(SC)or Identification card (IC) in order
to sit for and exam
SC OR IC
Example 2: Student must have both student card(SC)and Identification card (IC) in
order to sit for and exam
SC AND IC
Given : A OR B AND C OR B
AND OR NOT
C1 C2 R C1 C2 R C R
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F
Operation Resultant
Q1. Fill in the following table with the variable name and data type.
Q2. Name the data type for each of the following constant.
1. 5.38
2. “87645”
3. TRUE
4. “A”
5. “New York”
6. 2.45E6
7. 458963.62
1. F = A + B/C – D ^ 2
2. F = (A + B)/C – D ^ 2
3. F = A + B/(C – D ^ 2)
4. F = (A + B) MOD C
5. F = (A + B)\D ^ 2
Q4.
Set up a logical expression for the following conditions. A company gives a bonus
at the end of each fiscal year. For an employee to get a bonus, the following must be
true.
a. The employee has been working at the company for more than six
months with no negative reports.
b. The employee has earned more than $5,000 during the fiscal year.