You are on page 1of 21

CHAPTER 2:

BASIC ELEMENTS OF COMPUTER


PROGRAMS (Part 3)

Fundamentals of Algorithms and Computer Problem


Solving
OPERATOR

Comparison Relational Example


operators
RELATIONAL OPERATOR Less than < count<10
• Relational operators are used Less than or <= income<=2000
for comparing the values of
two operands. equal
Greater than > year>20
• The evaluation result is an
expression is either TRUE
(represented by non-zero) or Greater than or >= marks>=90
FALSE (represented by 0). equal
Equal == total==0

Not equal != num!=1


OPERATOR

RELATIONAL OPERATOR
Boolean Expression

• Is an expression in which two arithmetic values are COMPARED


using single relational operator.
• Each Boolean expression has the Boolean value TRUE or FALSE
according to arithmetic validity of the expression.
• Example:
• Given a = 4 and b = 10,
• The expression a < b is TRUE or 1
• The expression a == b is FALSE or 0
OPERATOR

LOGICAL OPERATORS
• When there is more than one RELATIONAL EXPRESSION
at a time, LOGICAL OPERATOR are used to perform the
evaluation.
• To perform logical operations, logical operator to be
used are shown below:
Logical operations Operators Example

AND && (a<b) && (a< c)

OR || (a==b) || (a==c)

NOT ! Num !=999


OPERATOR

LOGICAL OPERATORS
Truth Table
• A truth table is used to specify the conditions that make a
logical operation true and false.
• A truth table lists all possible combinations of operand values
and the result of the operation for each combination.
• The truth table for logical operation is summarized below:
X Y X && Y X || Y !X
(both X and Y must be (either X or Y must be (produces the
TRUE) TRUE) opposite relation)
True True True True False
True False False True False
False True False True True
False False False False True
OPERATOR

Operator category (highest to Operator (from left


lowest) to right)
Hierarchy of Operators Parenthesis ()
(Operator Precedence)
Multiply, divide, remainder */ %
• The operator Add, subtract
precedence determines + -
its order of evaluation in Relational operator < <= > >=
an expression. Equality operators == !=
• The following table Logical AND &&
shows the precedence Logical OR ||
from highest to lowest of
Assignment =
all common C++
operators.
ASSIGNMENT OPERATOR
• The assignation operator serves to assign a value
to a variable.

• Example:
a = 5;
• Assigns the integer value 5 to variable a.
• The part at the left of the = operator is known as
lvalue (left value) and the right one as rvalue
(right value).
• lvalue must always be a variable whereas the
right side can be either a constant, a variable,
the result of an operation or any combination of
them.
• It is necessary to emphasize that the assignation
operation always takes place from RIGHT TO
LEFT and NEVER AT THE INVERSE.
TEST AND DEBUG THE
PROGRAM

• A program is considered complete and ready to


implement only after it has been thoroughly
tested.
• Testing procedures involve running object
code with sample test data to determine if any
programming errors have been made.
• A mistake in a program à BUG
• The compiler will catch certain kinds of
mistakes and will write out an error message
when it finds a mistake.
• 3 kinds of program errors:
1. Syntax error
2. Logic error
3. Runtime error
TEST AND DEBUG THE
PROGRAM
Syntax error

• A syntax error violates the syntax rules of the


programming language in which it is written.
• e.g. A C++ program without “;” in the end of a
line.
TEST AND DEBUG THE
PROGRAM
Logic Error
• The compiler only tells you if you wrote
a syntactically correct program.
• It will not tell you whether the
program does what you want to do.
• A LOGIC ERROR is far more serious
than a syntax error, because it is more
difficult to locate and correct.
• A logic error may indicate faulty or
incomplete design; the programmer
may have to rethink the problem.
TEST AND DEBUG THE
PROGRAM
Run-time error
• Errors that the computer system can
detect only when a program is run.
• Most computer system will detect
certain run-time errors and output an
appropriate error message.
• Many run-time errors have to do with
numeric calculations.
• Eg. Dividing a number by 0
TEST AND DEBUG THE
PROGRAM
• Procedures that are carried out to
correct syntax and logic errors
are called DEBUGGING activities.
• A debugged program is one in
which all identified syntax and /
or logic errors have been
corrected so that test data
produce correct output.
• PROGRAM CONTROL
STRUCTURE controls the flow
of execution of program
statements.
• C++ provides structures that
PROGRAM will allow an instruction or a
block of instruction to be
CONTROL executed, repeated or
skipped.
STRUCTURE • There are THREE CONTROL
STRUCTURE:
1. Sequential Structure
2. Selection Structure
3. Repetition Structure
1. SEQUENTIAL STRUCTURE

• The sequential structure has


one entry point and one exit
PROGRAM point as shown below:
CONTROL Start -> S1 -> S2-> S3 -> S4 -> Exit
STRUCTURE
• Statements are executed in
sequence one after another
without leaving out any
single statement.
1. SEQUENTIAL STRUCTURE

• The following program show


the sequential structure:
PROGRAM
CONTROL int main()
{
STRUCTURE float payRate = 8.5;
float hourWorked = 25.0;
wages= hourWorked * payRate;
cout<<”\n Wages = $ “<<wages<<endl;
}
2. SELECTION STRUCTURE

• The selection structure is used


to allow choices to be made.
PROGRAM • C++ uses the if-else and
CONTROL switch statements for making
selection.
STRUCTURE
• The following shows a portion
of a program that uses the
selection structure.
2. SELECTION STRUCTURE

• The following shows a portion of a


program that uses the selection
structure.

PROGRAM int main()


{
CONTROL ……
STRUCTURE if (age > 10)
cout<<” Allowed to enter “;
else
cout<<”Not allowed”;
……
}
3. REPETITION STRUCTURE
• Statements are executed repeatedly
while certain condition remains true.
• In C++, while, do-while and for are the
statements used within the repetition
structure.
• The following shows the use of the
repetition structure in a portion of a
PROGRAM program:

CONTROL int main()


STRUCTURE {
…….
while (num > 0)
{
sum = sum + num;
cin>> num;
}
……..
}
EXERCISE

1. Write a C++ statement for the following:

i. Declare a double named salary


ii. Assigns 15 to x and y
iii. Add 1 to counter
iv. Decrease 2 from counter
v. Input the user’s age
vi. Initialize total to 0
vii. Declare variable named address to store 15 characters
EXERCISE

2. State whether the following variable name is VALID or


INVALID. If invalid explain why.
i. Net salary
ii. ID-NO
iii. 3numbers
iv. Sum2num
v. totalSalary$
vi. _kilo
vii. main
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