PHINMA – Cagayan de Oro College
College of Engineering and Architecture
ITE 295
Computer Programming
Laboratory Manual
Activity No. 2
Declaring and Using Variables and Constants
Objectives: 1. Declare and use variables and constant in a program;
2. Identify different kinds of data types.
Materials: Computer & C/C++ programming language software tool
Fundamental data types
Fundamental data types are basic types implemented directly by the language
that represent the basic storage units supported natively by most systems. They
can mainly be classified into:
Character types: They can represent a single character, such as 'A' or '$'. The
most basic type is char, which is a one-byte character. Other types are also
provided for wider characters.
Numerical integer types: They can store a whole number value, such
as 7 or 1024. They exist in a variety of sizes, and can either
be signed or unsigned, depending on whether they support negative values or
not.
Floating-point types: They can represent real values, such as 3.14 or 0.01,
with different levels of precision, depending on which of the three floating-point
types is used.
Boolean type: The boolean type, known in C++ as bool, can only represent
one of two states, true or false.
Declaration of Variables
The syntax to declare a new variable in C++ is straightforward: we simply write
the type followed by the variable name (i.e., its identifier). For example:
1 int a;
2 float mynumber;
These are two valid declarations of variables. The first one declares a variable of
type int with the identifier a. The second one declares a variable of type float with
the identifier mynumber. Once declared, the variables a and mynumber can be
used within the rest of their scope in the program.
If declaring more than one variable of the same type, they can all be declared in
a single statement by separating their identifiers with commas. For example:
int a, b, c;
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
This declares three variables (a, b and c), all of them of type int, and has exactly
the same meaning as:
1 int a;
2 int b;
3 int c;
Very important: The C++ language is a "case sensitive" language. That means that an
identifier written in capital letters is not equivalent to another one with the same name but
written in small letters. Thus, for example, the RESULT variable is not the same as
the result variable or the Result variable. These are three different identifiers identifying three
different variables.
Initialization of Variables
In C++, there are three ways to initialize variables. They are all equivalent and
are reminiscent of the evolution of the language over the years. But we will only
discuss the most commonly used initialization.
It is known as c-like initialization (because it is inherited from the C language),
consists of appending an equal sign followed by the value to which the variable is
initialized:
type identifier = initial_value;
For example, to declare a variable of type int called x and initialize it to a value of
zero from the same moment it is declared, we can write:
int x = 0;
Introduction to Strings
Fundamental types represent the most basic types handled by the machines
where the code may run. An example of compound type is the string class.
Variables of this type are able to store sequences of characters, such as words
or sentences. A first difference with fundamental data types is that in order to
declare and use objects (variables) of this type, the program needs to include the
header where the type is defined within the standard library (header <string>):
Procedure:
1. In your C++ environment, type the following program seen below
2. Run the program. Place the output on the space provided (output1).
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Output1 Output2
3. Try another sample program, run and place the output on the space provided
(output2)
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Any observations of the 2 outputs?
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
Declaring Named Constants
A named constant is similar to a variable, except it can be assigned a value
only once. You use a named constant when you want to assign a useful name
for a value that will never be changed during a program’s execution.
Sometimes, it is just convenient to give a name to a constant value:
1 const double pi = 3.1415926;
2 const char tab = '\t';
4. Using your name, course & year, school and address, construct a program to
output as seen below. Use the header string for each data.
Sample Dialogue
Name : Michelle B. Mamba
Course & Year : BS Computer Engineering – 1
School : PHINMA - Cagayan de Oro College
Address : Concepcion, Kapatagan , Lanao del Norte
5. Write your program on the space provided
Observations
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Conclusion
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Your program here:
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Rubric
1 2 3 4
Student spent too Student spent too Student spent an Student spent an
much time and/or much time and/or adequate amount of adequate amount of
Adequate Time too little time on too little time on time on computer time on computer
Spent on Activity entire computer parts of computer lab activity to ensure lab activity to ensure
lab activity. lab activity. good results. the best results.
Student put little to Student put little Student put a good Student put a great
no effort towards effort towards amount of effort deal of effort
Effort computer lab computer lab towards computer towards computer
activity activity. lab activity. lab activity.
Student completed Student completed Student completed Student completed
less than 1/2 of about 1/2 of the about 80% of the all of the computer
Completion of the computer lab computer lab computer lab activity lab activity by the
Task activity by the due activity by the due by the due date. due date.
date. date.
Responses and Responses and Responses and Responses and
information given information given information given information given
Reasonable are entirely are unreasonable are reasonable are very reasonable
Response and unreasonable in some areas of throughout most of throughout all of the
Information throughout the the activity. the activity. activity.
activity.
Neatness, Responses and Responses and Responses and Responses and
Readability, and information given information given information given information given
Legibility are entirely are unreadable are neat, readable, are very neat,
unreadable and and illegible and legible readable, and
illegible throughout most of throughout most of legible throughout all
throughout the the activity. the activity. of the activity.
activity.
Instructor: Percentage Total
Subject
Name: Code