Operators in C++
Dr. karam Hatim
University of Mosul
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
Introduction
• Operator represents an action.
• For example + is an operator
that represents addition.
• An operator works on two or more operands
and produce an output.
• For example 3+4+5 here + operator works on three
operands and produce 12 as output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3
Operators Types
• Arithmetic operator
• Relational and logical operator
• Bitwise operator
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4
Arithmetic operators
+ - *
/ %
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5
Examples about /
8 / 2 =
24 / 4 =
5 / 2 =
5.0 / 2 =
5/ 2.0 =
int / int = int
Double / int = double
Int / double =double
4
6
2
2.5
2.5
Examples about %
5 % 2 =
8 % 5=
14 % 10=
7 % 5 =
5 % 7=
5.0 % 2=
5 % 2.0 =
int % int
int % double
double % int
1
3
4
2
0
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7
Mixed operation
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 8
Complete example
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9
Increment ++/ Decrement --
X=10;
• y=x++;
• y=++x;
X=10;
• y=x--;
• y=--x;
What is the difference??
y=10 x=11
y=11 x=11
y=10 x=9
y=9 x=9
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10
Expressions
• If all operands are same type
• Example: 2 + 3 * 5
12.8 * 17.5 - 34.50
• Mixed expression:
− Has operands of different data types
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11
Mixed Expressions
• Evaluation rules:
− If operator has same types of operands
• Evaluated according to the type of the
operands
− If operator has both types of operands
• Integer is changed to floating-point
• Operator is evaluated
• Result is floating-point
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12
Type Conversion (Casting)
• Implicit type coercion:
when value of one type is automatically
changed to another type.
• Cast operator:
provides explicit type conversion
static_cast<dataTypeName>(expression)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13
Example: Type Conversion
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 14
Relational and Logical Operators
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15
Logical operator
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16
Bitwise Operators
• There are six bitwise Operators:
• & and
• | or
• ^ xor
• ~ not
• << shift left
• >> shift right
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17
Example of Bitwise Operators
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18
Operator Precedence in C++
• This determines which operator
needs to be evaluated first if an
expression has more than one
operator.
• Operator with higher precedence at
the top and lower precedence at the
bottom.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19
Unary Operators ++ – – ! ~
Multiplicative * / %
Additive + –
Shift << >> >>>
Relational > >= < <=
Equality == !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
Assignment = += -= *= /= %= > >= < <= &= ^= |=
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20
Thanks

Lecture3operatorsinC.ppsxkhhfhdgduhdufejbi

  • 1.
    Operators in C++ Dr.karam Hatim University of Mosul
  • 2.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 2 Introduction • Operator represents an action. • For example + is an operator that represents addition. • An operator works on two or more operands and produce an output. • For example 3+4+5 here + operator works on three operands and produce 12 as output.
  • 3.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 3 Operators Types • Arithmetic operator • Relational and logical operator • Bitwise operator
  • 4.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 4 Arithmetic operators + - * / %
  • 5.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 5 Examples about / 8 / 2 = 24 / 4 = 5 / 2 = 5.0 / 2 = 5/ 2.0 = int / int = int Double / int = double Int / double =double 4 6 2 2.5 2.5
  • 6.
    Examples about % 5% 2 = 8 % 5= 14 % 10= 7 % 5 = 5 % 7= 5.0 % 2= 5 % 2.0 = int % int int % double double % int 1 3 4 2 0
  • 7.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 7 Mixed operation
  • 8.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 8 Complete example
  • 9.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 9 Increment ++/ Decrement -- X=10; • y=x++; • y=++x; X=10; • y=x--; • y=--x; What is the difference?? y=10 x=11 y=11 x=11 y=10 x=9 y=9 x=9
  • 10.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 10 Expressions • If all operands are same type • Example: 2 + 3 * 5 12.8 * 17.5 - 34.50 • Mixed expression: − Has operands of different data types 2 + 3.5 6 / 4 + 3.9 5.4 * 2 – 13.6 + 18 / 2
  • 11.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 11 Mixed Expressions • Evaluation rules: − If operator has same types of operands • Evaluated according to the type of the operands − If operator has both types of operands • Integer is changed to floating-point • Operator is evaluated • Result is floating-point
  • 12.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 12 Type Conversion (Casting) • Implicit type coercion: when value of one type is automatically changed to another type. • Cast operator: provides explicit type conversion static_cast<dataTypeName>(expression)
  • 13.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 13 Example: Type Conversion
  • 14.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 14 Relational and Logical Operators
  • 15.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 15 Logical operator
  • 16.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 16 Bitwise Operators • There are six bitwise Operators: • & and • | or • ^ xor • ~ not • << shift left • >> shift right
  • 17.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 17 Example of Bitwise Operators
  • 18.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 18 Operator Precedence in C++ • This determines which operator needs to be evaluated first if an expression has more than one operator. • Operator with higher precedence at the top and lower precedence at the bottom.
  • 19.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 19 Unary Operators ++ – – ! ~ Multiplicative * / % Additive + – Shift << >> >>> Relational > >= < <= Equality == != Bitwise AND & Bitwise XOR ^ Bitwise OR | Logical AND && Logical OR || Assignment = += -= *= /= %= > >= < <= &= ^= |=
  • 20.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 20 Thanks