operators - wordpress.com...2015/02/04  · c++ operators • assignment operator • arithmetic...

24
Operators Budditha Hettige Department of Computer Science IT 1033: Fundamentals of Programming

Upload: others

Post on 21-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Operators

Budditha Hettige

Department of Computer Science

IT 1033: Fundamentals of Programming

Page 2: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Example

Budditha Hettige ([email protected])

What is the output of the following program

Page 3: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

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 4: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

C++ Operators

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

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

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

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

Arithmetic operators( +, -, *, /, % )

• The five arithmetical operations supported by C++

are

Budditha Hettige ([email protected])

Page 9: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Compound assignment

(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

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

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 11: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Example

Budditha Hettige ([email protected])

Page 12: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

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

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

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

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

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 17: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Example

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

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

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 20: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Example

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

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

Bitwise operators

( &, |, ^, ~, <<, >> )

Bitwise operators modify variables considering the bit

patterns that represent the values they store.

Budditha Hettige ([email protected])

Page 23: Operators - WordPress.com...2015/02/04  · C++ Operators • Assignment operator • Arithmetic operators • Compound assignment • Increment and decrement • Relational and comparison

Precedence of operators

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

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?