Unit 4
Unit 4
UNIT 4
OBJECTIVES
General Objective : To Introduce and Use the data types, operator and
expression in C++ Programming.
INPUT 4
The C++ character set includes the keyboard characters, namely the
uppercase letter A - Z, the lower letters a – z , the digits 0 – 9 , and other special
characters (such as!, #, [, ] , ,&,<,?, and * ). The blank space is also a character in
the set.
Identifiers are simply references to memory location which can hold values.
They are formed by combining letters (both upper and lowercase), digits and the
underscore( _ ). The first character of an identifier, however, must be a letter. The
blank character is not permitted in an identifier. Although identifiers can be formed
by freely combining the letters, digits and underscores, common sense tells us that
we should give them suggestive names, that is, names that reflect the data items that
they are going to store. Identifiers can be of any length although in practice they
seldom exceed 25 characters.
E3062/4/3
Table 4.1
Valid Identifier Invalid Identifier Comment
x “x” Illegal character
Sumx2 2sumx Illegal first character
Hourly_rate Hourly-rate Illegal character -
name name@ Illegal character @
GROSS_PAY GROSS PAY Illegal blank
i. int
ii. char
iii. double
iv. float
For additional of basic data types programming C++ can support another built-in
data types such as short, long, signed and unsigned. The various data types, the
number of bits required to store them, and their range are shown in Table 4.1
E3062/4/4
Table 4.2
Data Type C++ Keyword Bits Range
Integer int 16 -32768 to 32767
Long integer long 32 -42994967296 to 4294967295
Short integer short 8 -128 to 127
Unsigned integer unsigned 16 0 to 65535
character char 8 0 to 255
Floating point float 32 Approximately 6 digits of
precision
Double floating double 64 Approximately 12 digits of
point precision
This declaration means that the compiler was asked to hold integer
data name bilangan, nom1 and nom 2. Integer data types is classified as sign
(+) and unsigned (-).For all the computer system, data integer is refer to 0
and 1 bits. There are three types of integer short int, int, long int. The
comparison of this integer range in value.
This declaration means that variable lagi and huruf will given a
memory storage for character data type. Character variable store printed data
or un printed data in character set of computer includes;
small and capital letters
decimal digits (0-9)
special character
1. float
2. double
3. long double
E3062/4/6
The comparison of the three types are range value and digits.
Constant are values that do not change during program execution. They can be of
type integer, character or floating point. To declare a constant, use the keyword const
as in the example
i. Character constants:
‘$’ ‘*’ ‘ ‘ ‘z’ ‘G’
All variables in a program must be declared prior to their use. The declaration takes
the form
Type variable_list;
Int x, y, z;
Short small_number;
Long big_number;
Unsigned positive_number;
Char ch;
Float amount, rate;
E3062/4/8
Variables may be initialized by assigning constants to them either at the time of their
declaration or at a later time.
int m, n=10;
float rate, total = 0.0;
char response= ’n’;
char color{6}=’green’;
E3062/4/9
Activity 4A
Feedback To Activity 4A
4.1.
a. Data types: integer Keywords : int
b. Data types : character Keywords: char
c. Data types : double Keywords : double
d. Data types : floating point Keywords ; float
4.2.
a. constant : const Int hourly_rate
b. variable: Int a, b, c
E3062/4/11
INPUT 4
C++ is very rich in built-in operators. Operators trigger some computations when
applied to operands in an expression. There are several general classes of operators.
But for now, we will present just the arithmetic, relational, logical and assignment
operators.
Table 4.3
Operator Action
- Subtraction
+ Addition
* Multiplication
/ division
% Modulus division
- Decrement
++ increment
E3062/4/12
Examples of operation involving integer variables. With a=7 and b=2, the
expression on the values on the right.
Table 4.4
Expression Value
a–b 5
a+b 9
a*b 14
a/b 3
a%b 1
a-- 6
a-- 3
or - . Example:
a = -20 // assigns ‘a’ a negative 20
b = +20 // assigns ‘b’ a positive 20 ( + sign normally not
needed)
Unary operators operate on single value. Thus the size of operator is a unary
operator. So are the decrement and increment operator.
There are six relational operators and three logical operators in C++ are
shown in Table 4.5 and Table 4.6
E3062/4/13
Table 4.5
Operator Meaning
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal
!= Not equal
There are three logical operators in C++ are shown in Table 4.6
Table 4.6
Operator Meaning
&& AND
|| OR
! NOT
The result of the logical operations on a and b are summarized as Table 4.7:
Table 4.7
a||b a&&b !a
a b either a or b Both a and b Produces the
must be true Must be true opposite relation
0 0 0 0 1
0 1 1 0 1
1 0 1 0 0
1 1 1 1 0
E3062/4/14
*Hierarchy of Operators
The hierarchy of operator precedence form highest to lowest is summarized
below:
Table 4.8
Operator Category Operator
Unary - -- ++
Arithmetic multiply, devide, remainder + / %
Arithmetic add and subtract + -
Relational operators < <= > >=
Equality operators == !=
Logical AND &&
Logical OR ||
Gross_pay – deductions
(basic_pay + hours * rate) – (socso + premium + loan)
(b * b – 4 * a * c) > 0
(gender = = ‘m’) && (age > 20)
(gender = = ‘m’ || gender = = ‘f’ ) && age >= 21
E3062/4/15
Activity 4B
Feedback To Activity 4B
4.3.
a. arithmetic operators : - , + , ++ , *
b. logical operators : && , !
c. Relational operators : <= , != , = =
4.4.
( a + b * 2 ) – (x + y + z) – arithmetic
(sex = = ‘m’ || sex = = ‘f’ ) && age >= 21
E3062/4/17
KEY FACTS
SELF-ASSESSMENT
You are approaching success. Try all the questions in this self-assessment section
and check your answers with those given in the Feedback on Self-Assessment 14
given on the next page. If you face any problems, discuss it with your lecturer.
Good luck.
Question 4-1
Question 4-2
Using the statement that you write in statement for question no.1, write the complete
program to calculate and printed multiplication of the three integers.
E3062/4/19
Feedback To Self-Assessment
Have you tried the questions????? If “YES”, check your answers now.
Question 4-1
a. // Program to determine three integer multiplication
or
/* Program to determine three integers multiplication*/
b. int x, y, z, hasil;
c. cout << “Enter three integers number:”;
d. cin >> x >> y >> z;
or
cin >> x;
cin >> y;
cin >> z;
e. hasil = x * y * z;
f. cout << “Multipication is :” << hasil;
Question 4-2
/* Program to determine three integer multiplication*/
# include <iostream.h>
int x, y, z, hasil;
void main( )
CONGRATULATIONS
{ !!!!…..May success be
cout << “Enter 3 integer number:”; with you always….
cin >> x >> y >> z;
hasil = x * y * z;
cout << “Multipication is :” << hasil;
}