Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Programming

Concepts of Variables, Constant and Operators


Objectives
• Distinguish among a variable, a named constant, and a literal constant
• Explain how data is stored in memory
• Select an appropriate name, data type, and initial value for a memory
location
• Classify data types
• Use operators in programming
Variables and Constants
• Named locations in the memory of your computer
• A variable is a memory location whose value can
change (vary) during runtime, which is when a
program is running. Most of the memory locations
declared in a program are variables.
• A named constant is a memory location whose value
cannot be changed during runtime
Illustration
In a program that inputs the radius of any circle and then calculates and
outputs the circle’s area, a programmer would declare variables to
store the values of the radius and area; doing
this allows those values to vary while the program is running. However,
he or she would declare a named constant to store the value of pi (π),
which is used in the formula for calculating the area of a circle. (The
formula is πr2 .) A named constant is appropriate in this case because
the value of pi (3.141593 when rounded to six decimal places) will
always be the same.
Selecting a Name for a Memory Location
• Every memory location that a programmer declares must be assigned
a name.
• The name, also called the identifier, should describe the contents of
the variable.
• A good memory location name is one that is meaningful right after
you finish a program
Selecting a Name for a Memory Location
• It must begin with a letter and contain only letters, numbers, and the
underscore character.
• No punctuation marks, spaces, or other special characters (such as $
or %) are allowed in the name.
• The name cannot be a keyword, which is a word that has a special
meaning in the programming language you are using. Keywords are
also referred to as reserved words.
Exercise

•Tell whether if each of the


following is VALID or INVALID
1.
2018Sales
2.
end Balance
3.
first.name
4.
echo
5.
Rate%
Selecting a Data Type for a Memory Location
Except for the string data type, the data types
listed belong to a group of data types called
fundamental data types. The fundamental data
types are the basic data types built into a
programming language and often are referred to
as primitive data types or built-in data types.
Illustration
Setting an initial value
Declaring Variables and Constants
Stop and analyze

How many memory locations will the problem require?


Stop and analyze

How many of the memory locations will be variables, and how


many will be named constants? Why did you choose one type
over the other?
Stop and analyze

How would you write the appropriate declaration statements?


Use the int data type for the quantity sold, and the double data
type for the remaining input, processing, and output items.
Activity1. Give the right declaration for each of the following

1.1
Activity1. Give the right declaration for each of the following

1.2
3. Activity

3.1. Create the IPO chart


3.2. Write the correct declaration for your
variables and constants (if there are any)
Arithmetic Operators
Illustration
Illustration
Illustration
Sales = 1000;
SalesComm = 0.10;
Sales = Sales * SalesComm;

What is the values of Sales?


Illustration
X = 1+(12/4)*3

What is the value of X?


Illustration
Y = 16-10%4;

What is the value of Y?


Illustration
X = 100/4;
X=X*4;

What is the value of X?


Illustration
X = 100/4;
X=X++; (same with X=X+1;)

What is the value of X?


Activity 4
Give the value of X

4.1
Y=2500;
X = Y/5 + X;
Activity 4
Give the value of X

4.2
A=2;
B=A;
C=100;
X =C-A*B;
Activity 4
Give the value of X

4.3
X=2;
Y=3;
X =A + Y;
X=10;
Activity 4
Give the value of X

4.4
X=2;
Y=3;
X =Y;
Y=7;
X=X+Y;

You might also like