0% found this document useful (0 votes)
14 views39 pages

DFC20113 - Chapter 2 (Basic Program Elements - 2.4)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views39 pages

DFC20113 - Chapter 2 (Basic Program Elements - 2.4)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

DFC20113

PROGRAMMING FUNDAMENTALS

Chapter 2.4
Operator and Expression
Today we will...
Define operator.
Explain the types of operators:
Assignment operators
Arithmetic operators
Increment and decrement operators (Unary)
Relational operators
Logical operators
Conditional operators
Identify the syntax for each operator with example.
Differentiate the assignment (=) operator and equality (==) operator.
Write expression using operators.
Describe operators’ precedence.
Solve expression according to the operators’ precedence.
Write a simple program that apply operators and expression. Run test and debug the
program.
Operators are the foundation of
any programming language.

We can define operators as


symbols that help us to perform
specific mathematical and logical
computations on operands.

Operators includes:
Arithmetic's Operators, Assignment Operators,
Relational Operators , Logical Operators,
Increment and Decrement Operators and
Conditional Operators
Arithmetic's operators
(Mathematical
operators)
The five arithmetic operations supported
by the C++ language are:
Operations of addition, subtraction, multiplication
and division literally correspond with their
respective mathematical operators.

The only one that you might not be so used to


see is modulus; whose operator is the percentage
sign (%).

Modulus is the operation that gives the


remainder of a division of two values.
PROGRAM CODE
Assignment operators
Assignment Operator evaluates an
expression on the right of the expression
and substitutes it to the value or variable
on the left of the expression.

This statement assigns the


number = 5; integer value 5 to the
variable number.
PROGRAM CODE
Compound Assignment
When we want to modify the value of a variable by
performing an operation on the value currently
stored in that variable, we can use compound
assignment operators:
PROGRAM CODE
Relational Operators
Relational and equality operators are used to evaluate
a comparison between two expressions.

Commonly, relational expressions are used in decision


making statements of C++ language such as if, while and
for statements to decide the course of action of a
running program.

The result of a relational operation is a Boolean value


that can only be 1 - if true or 0 - if false, according to
its Boolean result
Relational Operator
PROGRAM CODE
Logical Operators
Logical Operator
Logical operator uses to test multiple conditions. There
are 3 types of logical operators and they work the
same way as the Boolean:
AND (&&)
This operation results true if both its two operands are true, and
false otherwise.

OR (||)
This operation results true if either one of its two operands is true,
thus being false only when both operands are false themselves.

NOT (!)
This operation has only one operand and inverse the value of it,
producing false if its operand is true and true if its operand is false.
AND (&&)
The following panel shows the result of operator
AND (&&) evaluating the expression a && b:
PROGRAM CODE
OR (||)
The following panel shows the result of operator
OR (||) evaluating the expression a || b:
PROGRAM CODE
NOT (!)
Basically, it returns the opposite Boolean value of
evaluating its operand. The following panel shows
the result of operator NOT (!):
PROGRAM CODE
Increment & Decrement
Operators
Increment & Decrement Operator
Shortening even more some expressions, the
increase operator (++) and the decrease operator
(--) increase or reduce by one the value stored in
a variable. They are equivalent to +=1 and to -=1,
respectively. Thus:
1. c++;
2.c+=1;
3.c=c+1;
Are all equivalent in its functionality: the three of
them increase by one the value of c.
The increment and decrement operator are
extensively used in for, do while and while loops. A
characteristic of this operator is that it can be
used both as a prefix and as a postfix.

++VARIABLE VARIABLE++
--VARIABLE VARIABLE--
prefix postfix
How to solve?
PROGRAM CODE

prefix
PROGRAM CODE

postfix
Conditional Operators
The conditional operator can exchange simple
if-else code for a single operator. This
operator is the only C++ ternary operator
(working on three values).

HOW TO WRITE?
conditional <Expression> ? <expression1>:<expression2>;

If the conditional Expression is TRUE,


expression1 executes, otherwise if the
conditional Expression is FALSE, expression
2 executes.
Syntax:
if (a>b)
(a>b)?(c=25) : (c=45); {
c=25;
}
else
c = (a>b) ? 25: 45; {
c=45;
}
PROGRAM CODE
Operator Precedence
Operator precedence means determines which operator is performed
first in an expression with more than one operators with different
precedence.
Operator Precedence in C++ Programming
PROGRAM CODE
What is Expression?
An expression in a programming language is a combination of values,
variables, operators, and functions that are interpreted (evaluated)
according to the particular rules of precedence and of association for
a particular programming language, which computes and then produces
another value.
Next class, we will apply these all
operators to solve a problem in a
programming code.

Thank you!

You might also like