![Page 1: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/1.jpg)
Operators
Budditha Hettige
Department of Computer Science
IT 1033: Fundamentals of Programming
![Page 3: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/3.jpg)
C++ Operators• Assignment operator
– (=)
• Arithmetic operators
– ( +, -, *, /, % )
• Compound assignment
– (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
• Increment and decrement
– (++, --)
• Relational and comparison operators
– ( ==, !=, >, <, >=, <= )
• Logical operators
– ( !, &&, || )
• Conditional ternary operator
– ( ? )
• Comma operator
– ( , )
Budditha Hettige ([email protected])
![Page 5: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/5.jpg)
Binary Operators
• The binary operators take two arguments as
operands
Budditha Hettige ([email protected])
Left Operand
RightOperand
Operator
![Page 6: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/6.jpg)
Unary Operators
The unary operators take one arguments as operand
Budditha Hettige ([email protected])
Operand Operator
![Page 7: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/7.jpg)
Assignment operator (=)
• The assignment operator assigns a value to a
variable.
Budditha Hettige ([email protected])
![Page 8: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/8.jpg)
Arithmetic operators( +, -, *, /, % )
• The five arithmetical operations supported by C++
are
Budditha Hettige ([email protected])
![Page 10: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/10.jpg)
Combined assignment operators
• Each arithmetic operator has a corresponding
assignment operator.
Budditha Hettige ([email protected])
Operator EffectL = Left Operator
R = Right Operator
+= Assign (L + R) to L
-= Assign (L - R) to L
*= Assign (L * R) to L
/= Assign (L / R) to L
%= Assign (L % R) to L
![Page 12: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/12.jpg)
Unary Operators
The unary operators take one arguments
- unary minus (negation)
+ unary plus
-- decrement
++ increment
Budditha Hettige ([email protected])
![Page 13: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/13.jpg)
Unary Operators
• The unary minus (-) makes a positive number into a
negative number and a negative number into a
positive number.
• The unary plus (+) does not change the number.
• The decrement operator (--) decrements the value of
its operand by 1.
• The increment operator (++) increments the value of
its operand by 1.
Budditha Hettige ([email protected])
![Page 14: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/14.jpg)
The prefix version (++x or --x)
• Comes before the operand, as in ++x
• First increments or decrements the variable by 1
and then uses the value of the variable.
Budditha Hettige ([email protected])
meansChange xThen assign to yy = 6, x = 6.
![Page 15: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/15.jpg)
The postfix version (x++ or x--)
• Comes after the operand, as in x++
• Uses the current value of the variable and then
increment or decrements the variable by 1.
Budditha Hettige ([email protected])
meansAssign z to y.Then change z.y is 5, z is 6.
![Page 16: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/16.jpg)
Relational and comparison operators
• The result of such an operation is either true or false
(i.e., a Boolean value)
Budditha Hettige ([email protected])
![Page 18: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/18.jpg)
Logical Operators
• To combine or modify existing expressions.
! NOT
&& AND
| | OR
• Example
a > 5 && b > 5
ch == ‘y’ | | ch == ‘Y’
!valid
!(x > 5)
Budditha Hettige ([email protected])
![Page 19: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/19.jpg)
Conditional ternary operator ( ? )
• The conditional operator evaluates an expression,
returning one value if that expression evaluates to
true, and a different one if the expression evaluates
as false.
• Syntax is:
condition ? result1 : result2
Budditha Hettige ([email protected])
![Page 21: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/21.jpg)
Comma operator ( , )
• The comma operator (,) is used to separate two or
more expressions
• has the lowest precedence
• is left-associative
• Example,
a = (b=3, b+2);
Budditha Hettige ([email protected])
![Page 22: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/22.jpg)
Bitwise operators
( &, |, ^, ~, <<, >> )
Bitwise operators modify variables considering the bit
patterns that represent the values they store.
Budditha Hettige ([email protected])
![Page 24: Operators - WordPress.com...2015/02/04 · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison](https://reader030.vdocuments.site/reader030/viewer/2022041107/5f0a33487e708231d42a7f27/html5/thumbnails/24.jpg)
Precedence of operators
Budditha Hettige ([email protected])
• Precedence: when an expression contains two different kinds of operators, which should be applied first?
• Associativity: when an expression contains two operators with the same precedence, which should be applied first?