COMP 112: COMPUTER PROGRAMMING 1
PROGRAMMING CONSTRUCT (VARIABLE, DATA
TYPES, INPUT/OUTPUT STATEMENTS) UNIT V
CONTENTS UNIT IV
❑Internal memory
❑Variables
❑Selecting a name, data type and initial value for a
memory location
❑Declaring a memory location
❑Input/output statements
❑Arithmetic and Assignment Operator
❑Code the algorithm into a program
❑Desk-check the algorithm
❑Evaluate and modify (if necessary) the program
INTERNAL MEMORY UNIT V
INTERNAL MEMORY
❑is a component found in every computer.
❑is divided into memory regions, each of which
has a distinct numeric address.
❑can be thought of as storage bins, but unlike
storage bins, each storage bin (memory location)
inside a computer can only keep one item at a
time.
INTERNAL MEMORY
❑As the program runs, the memory locations will
record the values of the input, processing, and
output items.
❑Declaring a memory location is similar to
reserving a memory location.
❑ A memory location is declared using a C++
instruction that gives it a name, data type, and
(optionally) an initial value.
VARIABLES UNIT V
C++ VARIABLES
❑A variable is a named storage location that holds a
value.
❑It's a fundamental concept in programming that
allows you to store, retrieve, and manipulate data.
Think of a variable as a container or a box with a label on it.
The label is the variable's name, and the box holds the data.
The value inside the box can be changed, or "varied," during a
program's execution, which is where the name "variable" comes
from.
SELECTING A NAME FOR A
UNIT V
MEMORY LOCATION
SELECTING A NAME FOR A MEMORY
LOCATION
❑The name, also known as the identifier, should
be descriptive in order to help you remember the
purpose of the memory location.
❑A good name for a memory location is one that is
meaningful when you finish a program.
EXAMPLE OF EXAMPLE OF
HOW TO NAME A MEMORY LOCATION
VALID NAMES IN INVALID NAMES
IN C++
C++ IN C++
Sales2011 2011Sales
1. The NAME must begin with a letter.
firstNumber 1stNumber
2. The NAME can contain only letters,
Tax_rate Tax-rate
numbers, and the underscore character.
num_1 num 1
Note: No punctuation marks, spaces, or
other special characters (e.g. !, @, #, $, &)
ratePercentage rate%
are allowed in the NAME.
return, int, float,
3. The NAME cannot be a keyword in C++.
double, break
EXAMPLE OF EXAMPLE OF
HOW TO NAME A MEMORY LOCATION
VALID NAMES IN INVALID NAMES
IN C++
C++ IN C++
4. NAME in C++ are case sensitive. discount is
different to
DISCOUNT or
Explanation: Discount
▪ This means that, in addition to using the
correct spelling, you must also use the Hello is different to
“exact case” when referring to a specific hello or HELLO
memory address in a program.
▪ To put it another way, if you declare a
memory location named discount at the
start of a program, you must refer to it as
discount throughout the program rather
than Discount or DISCOUNT.
The standard reserved keywords are:
asm, auto, bool, break, case, catch, char, class, const,
const_cast, continue, default, delete, do, double, dynamic_cast,
else, enum, explicit, export, extern, false, float, for, friend,
goto, if, inline, int, long, mutable, namespace, new, operator,
private, protected, public, register, reinterpret_cast, return,
short, signed, sizeof, static, static_cast, struct, switch,
template, this, throw, true, try, typedef, typeid, typename,
union, unsigned, using, virtual, void, volatile, wchar_t, while
REVISITING RICKY’S PROBLEM
Input Processing Output
Processing Item:
current daily pay daily raise new daily pay
raise rate Algorithm:
1. Start
2. Enter the current daily pay and raise rate.
3. Calculate the daily raise by multiplying
the current daily pay by the raise rate.
4. Calculate the new daily pay by adding the
daily raise to the current daily pay.
5. Display the new daily pay
6. End
REVISITING RICKY’S PROBLEM
A
Flowchart: Start
Enter the current new daily pay = daily raise *
daily pay and raise current daily pay
rate
Display the
daily raise = current daily new daily pay
pay * raise rate
A End
HOW TO NAME A MEMORY VALID Variable
IPO Chart Item
LOCATION IN C++ Name
1. The NAME must begin with a letter. Current daily pay cur_dailyPay
2. The NAME can contain only letters,
numbers, and the underscore character.
Raise rate raise_Rate
Note: No punctuation marks, spaces, or
other special characters (e.g. !, @, #, $,
&) are allowed in the NAME.
3. The NAME cannot be a keyword in C++.
Daily raise dailyRaise
4. NAME in C++ are case sensitive. New daily pay
new_dailyPay
CHOOSING A MEMORY
UNIT V
LOCATION’S DATA TYPE
DATA TYPE(S)
❑A data type is a classification that tells the
compiler how a programmer intends to use data.
❑It determines the kind of values a variable can
hold, the amount of memory it needs, and the
types of operations that can be performed on it.
PRIMITIVE DATA TYPE(S)
❑These are the most basic building blocks. They
hold a single value and are the foundation for
more complex data structures.
A Data Structure is a way of organizing and storing data in a
computer so it can be accessed and used efficiently.
PRIMITIVE DATA TYPE(S)
Memory
Data Type Meaning Stores Example
Required
Stores integers An integer range of:
int (whole numbers), -2,147,483,648 to 4 bytes 123 or -123
without decimals 2,147,483,648
float Stores floating point A number with 6 4 bytes 19.99 or -19.99
numbers, with digits of precision
decimals range:
-3.4×10308 to
3.4×10308
double Stores floating point A real number with 8 bytes 19.99 or -19.99
numbers, with 15 digits of precision
decimals range:
-1.7×10308 to
1.7×10308
PRIMITIVE DATA TYPE(S)
Memory
Data Type Meaning Stores Example
Required
char Stores single One character 1 byte ‘a’
characters ‘B’
Char values are ‘’
surrounded by single A blank space is a
character and is written
quotes as ' ', with a space
between the single
quotation marks.
bool A Boolean value True or false 1 byte True
(either true or false) False
DATA TYPE(S)
A Class from the C++ Standard Library
Memory
Data Type Meaning Stores Example
Required
string Stores text Zero or more 1 byte per "Hello World"
String values are characters character
surrounded by double
quotes
This means it's a complex data type, or an object, that's built on top of the primitive
types.
REVISITING RICKY’S PROBLEM
VALID Variable Initial Value
IPO Chart Item Data Type
Name (optional)
Current daily pay cur_dailyPay double 0.0
Raise rate raise_Rate double 0.0
Daily raise dailyRaise double 0.0
New daily pay
new_dailyPay double 0.0
SYNTAX IN PROGRAMMING UNIT V
SYNTAX IN PROGRAMMING
❑Syntax in programming refers to the set of rules
that define the combinations of symbols and
words that are considered to be correctly
structured statements or expressions in a
particular programming language.
❑It's essentially the grammar of a programming
language, dictating how code must be written for
the computer to understand it.
KEY ASPECTS OF SYNTAX
❑KEYWORD: These are reserved words with special
meanings (e.g. return, int, float, if, else, while)
❑OPERATORS: These are symbols that perform operations
(e.g. +, -, *, /, %, and =)
❑DELIMETERS: These are characters or symbols used to
group or separate parts of the code, like parentheses (),
brackets [], and braces {}.
❑PUNCTUATION: This includes characters like semicolons
(;), or commas(,) that are used to terminate or separate
statements.
TYPES OF ERROR
❑Syntax Errors: This is the most common type of
error for beginners and happens when the
compiler or interpreter can't understand the code
you've written because it doesn't follow the
language's grammatical rules.
❑For example, forgetting a semicolon(;) at the
end of a statement will result in a syntax error.
TYPES OF ERROR
❑Logic Errors: occurs when the code is
syntactically correct but doesn't do what the
programmer intended it to do.
❑For example, if you write a program to add two
numbers but accidentally use the subtraction
operator, it's a logic error, not a syntax error.
DECLARING A VARIABLE IN C++ UNIT V
DECLARING A VARIABLE IN C++
Syntax:
dataType variableName = initialValue;
❑All variables must be declared with a name and a data type
before they can be used in a program.
❑One rule in C++ is that all statements must end with a semicolon.
❑Another rule is that the programmer must provide a data type and
name for the variable being declared.
❑You can also provide an initial value for the variable.
DECLARING A VARIABLE IN C++
Syntax:
dataType variableName = initialValue;
Examples:
int age = 0;
double price = 0.0;
bool paid = false;
char grade = ‘A’;
string company = “XYZ Company”;
DECLARING A VARIABLE IN C++
• Several variables of the same type may be declared in one
declaration or in multiple declarations. We could have
declared all three variables in one declaration by using a
comma-separated list as follows:
From this to this
REVISITING RICKY’S PROBLEM
IPO Chart VALID Variable Initial Value C++ Statement:
Data Type
Item Name (optional) Variable Declaration
Current daily
cur_dailyPay double 0.0 double cur_dailyPay = 0.0;
pay
Raise rate raise_Rate double 0.0 double raise_Rate = 0.0;
Daily raise dailyRaise double 0.0 double dailyRaise = 0.0;
New daily
pay new_dailyPay double 0.0 double new_dailyPay = 0.0;
DECLARING A CONSTANT VARIABLE
IN C++
❑ A constant name (or constant variable) in programming is a name
given to a memory location that holds a value which cannot be
changed after it has been initialized. Once you assign a value to a
constant, it remains fixed for the entire duration of the program's
execution.
Syntax: const datatype constantName = value;
Example:
const float PI = 3.14159; const char ANSWER = ‘Y’;
const int MAX_AGE = 75; const bool ENROLLED = false;
const string SCHOOL = “ESSU Salcedo Campus”;
VARIABLE AND CONSTANT VARIABLE
❑ A Variables and named Constants are the
two sorts of memory regions that a
programmer might set apart.
INPUT/OUTPUT STATEMENTS UNIT V
INPUT/OUTPUT STATEMENTS
❑In C++, Input/Output (I/O) statements are used to
interact with the user or external files. This is
primarily handled by the <iostream> library, which
provides the cin and cout objects.
OUTPUT STATEMENTS
❑The cout (console output) object is used to display
information on the screen. It is typically used with the
insertion operator (<<), which directs data to the output
stream.
INPUT STATEMENTS
❑The cin (console input) object is used to read data from
the user. It is used with the extraction operator (>>),
which takes data from the input stream and stores it in a
variable.
EXTRACTION OPERATOR (>>)
❑The extraction operator in C++ is the >> symbol, which
is used with the cin object to read data from an input
stream and store it in a variable. It extracts data from
the input stream and places it into the specified
variable.
ARITHMETIC and ASSIGNMENT
UNIT V
OPERATORS
ARTIHMETIC OPERATORS
❑C++ has a set of Arithmetic operators that perform
basic mathematical calculations. These operators are
used to manipulate numerical values (literals and
variables) and return a new value.
ARTIHMETIC OPERATORS
ARTIHMETIC OPERATORS
ASSIGNMENT OPERATOR
❑The Assignment operator in C++ is the single equals
sign (=). Its purpose is to assign a value to a variable.
It takes the value on its right-hand side and stores it
in the variable on its left-hand side.
REVISITING RICKY’S PROBLEM
IPO Chart VALID Variable Initial Value C++ Statement:
Data Type
Item Name (optional) Variable Declaration
Current daily
cur_dailyPay double 0.0 double cur_dailyPay = 0.0;
pay
Raise rate raise_Rate double 0.0 double raise_Rate = 0.0;
Daily raise dailyRaise double 0.0 double dailyRaise = 0.0;
New daily
pay new_dailyPay double 0.0 double new_dailyPay = 0.0;
Revisiting Ricky’s C++ Statement
Problem
(IPO Chart)
Input: double cur_dailyPay = 0.0;
current daily pay double raise_Rate = 0.0;
raise rate
Processing Item:
daily raise double dailyRaise = 0.0;
Output:
new daily pay double new_dailyPay = 0.0;
Or you can write it as:
double cur_dailyPay, raise_Rate, dailyRaise, new_dailyPay;
Revisiting Ricky’s Problem C++ Statement
(IPO Chart)
Algorithm:
Enter the current daily pay and raise rate. cout<<“Enter the current daily pay: ”;
cin>>cur_dailyPay;
cout<<“Enter the raise rate: ”;
cin>>raise_Rate;
Calculate the daily raise by multiplying the dailyRaise = cur_dailyPay * raise_Rate;
current daily pay by the raise rate.
Calculate the new daily pay by adding the new_dailyPay = dailyRaise + cur_dailyPay ;
daily raise to the current daily pay.
Display the new daily pay cout<<“Your new daily pay is ”<<new_dailyPay;
Output: