You are on page 1of 17

CHAPTER 2:

BASIC ELEMENTS OF COMPUTER


PROGRAMS (Part 2)

Fundamentals of Algorithms and Computer Problem


Solving
CONSTANT
• Constant are identifiers with fixed value and do not change throughout the
program execution.
• The general form of the constant declaration and definition is

const data_type variable_name;

const int MONTHINYEAR=12;

const float PI=3.142;


CONSTANT
• It is common practice to use CAPITAL LETTERS for constant names
distinguish them visually from other variables.
• Once declared as constants, the variables cannot be modified
within program.
• Otherwise, it will cause a syntax error.
DATA TYPE
• Identifiers that store data, such as variables
and constants must be given the DATA TYPE.
• The data types specify the type and size of
data identifiers can hold.
• Generally, there are several data types
available as follow:

1. NUMERIC DATA TYPE


• Integer (int, long int)
• A whole number, which may be +ve, zero or –ve.
• Integers vary in size between systems.
• Therefore, to process integers outside the range
of -32768 to 32767, long integers are normally
used.

• Floating point (float, double)


• Data of type float ranges approximately 6 to 7
digits of precision whilst double takes
approximately 13 decimal digits of precision.
DATA TYPE
2. CHARACTER DATA TYPE
• Single character (char)
• If a variable is declared as char data type, it can
only store one character value (a letter, digit or
special symbol).
• In C++ the single character is enclosed in a single
quotation mark - ‘ ‘.
• Example:

char grade =’A’; //stores character A into variable grade


DATA TYPE
2. CHARACTER DATA TYPE
• String of characters (char [ ] )
• In C++, a string is defined as a sequence of
characters that is terminated by a null character,
‘\0’ which has the value of zero.
• The null character denotes the end of a string
and is added to the string by the compiler
automatically.
• Because of the null terminator, it is necessary to
declare a string with one character longer than
the actual size it can hold.
• If the size of a string is shorter than the value is
stored into the string, the value will be
truncated.
• For example, to declare a string that could hold 5
characters, the following statement is written.

char text[8] = “Welcome”;


// 8 is one character longer than 7
DATA TYPE
2. CHARACTER DATA TYPE
• In the following graphical representation, the
string text can be represented as AN ARRAY of
characters where the first index of an array
begins with the INDEX 0.

‘W’ ‘e’ ‘l’ ‘c’ ‘o’ ‘m’ ‘e’ ‘\0’

Index [0] [1] [2] [3] [4] [5] [6] [7]

• Value for the strings are enclosed in quotation


marks ( “ “ ), which are not themselves part of
the string.
• Blank spaces and characters other than letters
are also INCLUDED AS PART OF THE STRING, if
used.
DATA TYPE
• CONSTANT DATA TYPE
• Constants are value that are FIXED and do not
change throughout the program execution.
• Example:
const int days_a_week = 7;

• BOOLEAN DATA TYPE


• The data type bool has only TWO values: TRUE
and FALSE.
• Also, true and false are called the LOGICAL
(BOOLEAN) VALUES.
• The central purpose of this data type is to
MANIPULATE LOGICAL (BOOLEAN)
EXPRESSIONS.
DATA TYPE
Data types Keyword (C++) Bits Range Examples
Integer int 16 -32768 to 32767 45, 0, -10
Long integer long 32 -4294967296 to 34567, -45676,
4295967295 124566789
Character char 8 0 to 255 ‘x’, ‘N’, ‘$’
String char [ ] “hello”
“Malik”
“ Student id”
Floating point float 32 Approximately 6 32.88, -89.56
to 7 digits of
precision
Double floating double 64 Approximately 12.789987845,
point 13 digits of -25.12345678
precision
Constant const Pi=3.142
Boolean bool True
False
OPERATORS
• An operator is a SYMBOL that tells
the compiler to perform specific
mathematical or logical
manipulations.

• C++ has the following operators:


1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
OPERATORS
1. ARITHMETIC OPERATORS
• Integers and real numbers may be added,
subtracted, divided and multiplied.
• To perform arithmetic operations, the
arithmetic operators are used.
• The following table summarizes the arithmetic
operators.

Arithmetic operations Arithmetic operators


Subtraction -
Addition +
Multiplication *
Division /
Modulus division %
Decrement by 1 --
Increment by 1 ++
OPERATORS

1. ARITHMETIC OPERATORS

Syntax form:
OPERAND OPERATOR OPERAND
X + Y

• In performing an operation, the following RULES must be


considered:
• If BOTH OPERANDS are INTEGER, then the RESULT is an
INTEGER.
• If ANY OPERAND is a floating-point value, the RESULT is a
floating point.
** IMPORTANT**
OPERATORS

1. ARITHMETIC OPERATORS

• INTEGER DIVISION
• Dividing an integer by another integer will result in an
integer result.
• Thus 12/5 is 2 and NOT 2.4.
• Because integer cannot contain fractional part, a result
such as 2.5 cannot be obtained. In C++, the fractional part
of the result is dropped.
• To CAPTURE THE REMAINDER OF AN INTEGER DIVISION,
C++ provides a MODULUS OPERATOR (represented by
SYMBOL %) to be implemented only for integers.
• Thus 5 % 2 is 1.
OPERATORS
Example:
• Given int a=7, b=2;
• The following shows the memory content and the
result after the execution of the statement.

Expression Memory content Value


a-b 7-2 5
a+b 7+2 9
a*b 7*2 14
a/b 7/2 3
a%b 7%2 1
a-- 7-1 6
b++ 2+1 3
OPERATORS
• MORE THAN ONE OPERATOR can be
COMBINED to form one arithmetic expression.
• For example, sum = num1/num2 * 3 + 4;
• In C++ the order of evaluating the arithmetic
operation becomes important. The precedence
of evaluation from highest to lowest is
summarized below:
Operator Operation Precedence
() Parenthesis Evaluated first. If nested parenthesis, the innermost
pair is evaluated first.
If not nested, and on the same level, it is evaluated left
to right.
*/ % Multiply, division, Will be evaluated next, from left to right
modulus
+ - Add, sub Will be evaluated next, from left to right
= assignment Will be last evaluated from left to right
OPERATORS
• Based on the above hierarchy of operators, the
statement

Total = 8 / 2 * 3 + 4 * 5 % 3;

• Will be processed as follows:

Steps Operations Result


1 8/2 4
2 4*3 12
3 4*5 20
4 20 % 3 2
5 12 + 2 14
References:

Problem solving With C++ (Norizan Mohamad &


Mazidah Puteh (UiTM)

Liang, Y.D., Introduction to Programming with C++,


2nd edition, Pearson Higher Education, 2010

You might also like