DFC20113 - Chapter 2 (Basic Program Elements - 2.4)
DFC20113 - Chapter 2 (Basic Program Elements - 2.4)
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.
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.
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>;
Thank you!