0% found this document useful (0 votes)
38 views

02 C++ Operators

C++ operators allow performing arithmetic, relational, logical, and other operations. There are binary operators that take two operands and unary operators that take one operand. Expressions combine variables, literals, and operators to evaluate to a value. Operator precedence and associativity determine the order of evaluation when multiple operators are used in an expression.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

02 C++ Operators

C++ operators allow performing arithmetic, relational, logical, and other operations. There are binary operators that take two operands and unary operators that take one operand. Expressions combine variables, literals, and operators to evaluate to a value. Operator precedence and associativity determine the order of evaluation when multiple operators are used in an expression.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ Operators

Arithmetic, Relational, Logical, Increment/Decrement and Precedence


of Operators
Operators and Expression
• Operators are special symbols used for
• Arithmetic calculations
• Assignment statements
• Logical comparisons
• Expression can be combination of variables, literals and operators that
result in value
• Operands are arguments
that operators perform Operand 14 + 5 – 4 * ( 5 – 3 ) Expression
operations on
Operator
Arithmetic Operators
• The arithmetic operators are all binary operators
• i.e. operators that take two operands
• Each operand can be either a literal , a variable identifier , or an
expression.
• It is better for the two operands to be of the same data type, otherwise:
• The compiler will perform implicit casting (with literal values) and/or,
• Explicit casting need to be specified (with variables)
• if we divide two integers we get an integer result.
• if one or both operands is a floating-point value we get a floating-point
result.
Implicit V/S Explicit Type casting
• Implicit casting: is an automatic type • Explicit casting: converting an
conversion by the compiler.
• If operands of mixed types are used, the
expression to a different type by
compiler will convert one operand to agree writing the desired type in
with the other. To do this, it uses a hierarchy parentheses in front of the
of data types:
• Long double (highest)
expression or the variable.
• Double • Example:
• Float
• Unsigned long int • int nValue1 = 10;
• Long int • int nValue2 = 4;
• Unsigned int
• Int (lowest)
• float fValue = (float) (nValue1 /
• Boolean, character, wide character, enumeration, nValue2);
and short integer data types are promoted to int
Writing Mathematical formulas
• Always specify multiplication explicitly by using the operator * where
needed.
• Example: b² - 4 a c = b * b - 4 * a * c
• Use parentheses when required to control the order of operator
evaluation.
• Example: = (a+b)/(c+d)
• Two arithmetic operators can be written in succession if the second is
a unary operator
• Example: a x - ( b + c ) = a * - ( b + c )
Arithmetic Precedence
Assignment Operator
• The assignment operator assigns (=) the value on the right side of
operator to a variable on the left side of the operator
• Right side value may be from:
1. Literal: e.g. i = 1;
Assignment Operator
2. Variable identifier: e.g. start = i;
3. Expression: e.g. sum = first + second;
leftSide = rightSide ;

It is always a variable It is either a literal , a variable


identifier. identifier , or an expression.
Compound Assignment Operator
• 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

Syntax:
It is either a literal , a
leftSide Op= rightSide ; variable identifier , or
an expression.

Always it is a variable
identifier.
It is an operator.

Above is equivalent to:


leftSide = leftSide Op rightSide ;
Compound Assignment Examples
Increment / Decrement Operators
Sample Explanation
• ++ and -- are a unary operators that can be Expression
applied to variables to increment/decrement
the value they hold. ++ ++x Increment x by 1, then use the
new value of x in the expression
• x++/++x; is equivalent to x = x+1; in which x resides
• x--/--x; is equivalent to x = x-1; ++ x++ Use the current value of x in the
expression in which x resides and
• Pre-Increment/Decrement(++x/--x) then increment x value by 1
• Placing the increment/ decrement operator
before the variable name -- --x Decrement x by 1, then use the
new value of x in the expression
• Post-Increment/Decrement(x++/x--) in which x resides
• Placing the increment/ decrement operator -- x-- Use the current value of x in the
after the variable name expression in which x resides and
then decrement x value by 1
Relational and Equality Operators
• In order to evaluate a comparison between two expressions we can use the relational
and equality operators
• Each operand can be either a literal , a variable identifier , or an expression.
• The result of a equality and relational operation is a Boolean value that can only be true
or false.
Algebraic equality or C++ Equality or relational Example of C++ Meaning of the Condition
Relational Operator Operator Condition
== x == y x is equal to y
!= x != y x is not equal to y
> x>y x is greater than y
< x<y x is less than y
>= x >= y x is greater than or equal to y
<= x <= y x is less than or equal to y
Relational Operators Examples
2 == 4 Evaluates to false
5>2 Evaluate to true
3 != 4 Evaluate to true
8 >= 15 Evaluates to false
6 < 12 Evaluate to true
34 <= 150 Evaluates to true

Instead of using only numeric constants, we can use any valid


expression, including variables. Suppose that a=2,b=3 and c=6
Logical Operators
• They are used to combine two or more conditions or to complement the evaluation of the
original condition under consideration.
• Logical operators are used to perform logical AND, OR and NOT operations on Boolean
operands
• Logical AND operator: The ‘&&’ operator returns true when both the conditions under
consideration are satisfied. Otherwise, it returns false. For example, a && b returns true
when both a and b are true (i.e. non-zero).
• Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions
under consideration is satisfied. Otherwise, it returns false. For example, a || b returns
true if one of a or b, or both are true (i.e. non-zero). Of course, it returns true when both a
and b are true.
• Logical NOT operator: The ‘!’ operator returns true the condition in consideration is not
satisfied. Otherwise, it returns false. For example, !a returns true if a is false, i.e. when a=0.
Not Truth table AND truth table OR truth table
A B A && B A B A || B
A !A
True True True True True True
True False True False False True False True
False True False False True True
False True
False False False False False False

Operator Meaning Example Return value


&& Logical And (9>2) && (6>4) 1

|| Logical OR (9>2) || (3.4) 1

! Logical Not 4!4 0


Operator Precedence and Associativity
Precedence and Associativity of C Operators
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right

prefix ++ and prefix –– sizeof & * + – ~ ! Unary Right to left

typecasts Unary Right to left


*/% Multiplicative Left to right
+– Additive Left to right
< > <= >= Relational Left to right
== != Equality Left to right
&& Logical-AND Left to right
|| Logical-OR Left to right
?: Conditional-expression Right to left
= *= /= %=
+= –= <<= >>= &= Simple and compound assignment Right to left
^= |=
, Sequential evaluation Left to right

You might also like