Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Session 3

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 1 of 33

Session objectives (1)


Identify operators
Assignment Arithmetic Compound Assignment Relational Logical Bitwise

Understand precedence and evaluation of operators


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 2 of 33

Session objectives (2)


Discuss mixed mode expressions and implicit type conversions Understand type casting and type compatibility Identify C++ shorthand operators
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 3 of 33

Introduction
Operators are:
Are a set of characters

 Have specific meaning particular to a language  Do not include special characters ; : ,

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 4 of 33

Operands

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 5 of 33

Binary Arithmetic Operators (1) Require two operands

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 6 of 33

Binary Arithmetic Operators (2)


Adds the values of operands

result =

The addition operator


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 7 of 33

Binary Arithmetic Operators (3)


Subtracts the second operand from first

answer =

The subtraction operator


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 8 of 33

Binary Arithmetic Operators (4)


Multiplies the operands

ans =

The multiplication operator


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 9 of 33

Binary Arithmetic Operators (5)


Divides the first operand by the second

result =

The division operator


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 10 of 33

Binary Arithmetic Operators (6)


Store the remainder after integer division

answer =

The modulus operator


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 11 of 33

Binary Arithmetic Operators (7)


void main(void) { :

cout << endl ;

f_dist = f_speed * f_time ;

Multiplication operator

cout<<"The distance covered by the vehicle = " ; cout<< f_dist ; cout << endl ; }

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 12 of 33

Unary arithmetic operators (1) Require one operand


Negation operator Decrement operator Increment operator

int x; int x= -3 puls - - OR - - puls stnd ++ OR ++stnd

-++

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 13 of 33

Unary arithmetic operators (2)


#include <iostream.h> void main(void) { int value ; value = 1 ; cout cout cout cout cout cout cout cout << << << << << << << << "THE INCREMENT OPERATOR" << endl ; "Value of value: " << value << endl ; "Pre-fix increment operator (++value): " << ++value ; endl ; "Value of value: " << value << endl ; "Post-fix increment operator (value++): " << value++ ; endl ; "Value of value: " << value << endl ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 14 of 33

Unary arithmetic operators (3)


cout << "\n\nTHE DECREMENT OPERATOR" << endl ; cout << "Value of value: " << value << endl ; cout << "Pre-fix decrement operator (--value): " << --value ; cout << endl ; cout << "Value of value: " << value << endl ; cout << "Post-fix decrement operator (value--): " << value-- ; cout << endl ; cout << "Value of value: " << value << endl ; }

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 15 of 33

Assignment operator

Assignment operator

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 16 of 33

Multiple assignment
int var_1, var_1 = 70 var_2 = 70 var_3 = 70 var_2, var_3 ; ; ; ;

var_1 = var_2 = var_3 = 70 ; int var_1 = int var_2 = int var_3 ;

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 17 of 33

Relational operators (1)

- Comparative action on data - test relationship bet een variables


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 18 of 33

Relational operators (2)


== > < != >= <= Equal to Greater than Less than Not equal to Greater than or equal to Less than or equal to

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 19 of 33

Relational operators (3)


Equal to b == 3 b== c Greater than b>3 b>c Less than b<3 b<c Less than Equal to b <= 3 b <= c Greater than Equal to b >= 3 b>= c Not equal to b!= 3 b!= c
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 20 of 33

Logical operators (1)

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 21 of 33

Logical operators (2)


qual == 12 && age == 18

qual == 10 || age == 18

( ! ( age >= 18) )


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 22 of 33

Precedence and Order of Evaluation

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 23 of 33

Mixed mode Expressions & Implicit Type conversion

X = 5 * 6.7 ;
intege r float

No error
Integer converted to double automatically
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 24 of 33

Conversion table

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 25 of 33

Type conversion (1)

Integer values

Floating point variables

Value is not changed


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 26 of 33

Type conversion (2)

Integer values

Floating point variables

Value is changed
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 27 of 33

Adding integers to characters

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 28 of 33

Type casting (1) Often leads to


Use explicit type conversions in mixed mode expressions

float a ; (int) a ;
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 29 of 33

Type casting (2) float a=10.0, b= 3.0, c; c= a / b; Ans= 3.333333


c = (int) a / (int) b OR c = (int) (a / b)

Ans= 3
Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 30 of 33

C++ Shorthand Operators (1)


Help in reducing the code required in programming constructs

test_val = test_val + 5; test_val += 5;


Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 31 of 33

C++ Shorthand Operators (2)


Every shorthand operator consists of An arithmetic operator

+, -, /, *, %

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 32 of 33

C++ Shorthand Operators (3)

Linux, C, C++ / Object Oriented Programming with C++ / Session 3 / 33 of 33

You might also like