C++ Session 3

33
Session 3

Transcript of C++ Session 3

Page 1: C++ Session 3

Session 3

Page 2: C++ Session 3

Session objectives (1)• Identify operators

• Assignment• Arithmetic• Compound • Assignment• Relational• Logical • Bitwise

•Understand precedence and evaluation

of operators

Page 3: C++ Session 3

•Discuss mixed mode expressions

and implicit type conversions

•Understand type casting

and type compatibility

• Identify C++ shorthand operators

Session objectives (2)

Page 4: C++ Session 3

IntroductionOperators

are: • Are a set of characters

• Have specific meaning particular to a

language • Do not include special characters ; “ : ,

Page 5: C++ Session 3

Operands

Page 6: C++ Session 3

Binary Arithmetic Operators (1)

Require two operands

Page 7: C++ Session 3

Binary Arithmetic Operators (2)

Adds the values of operands

result =

The addition operator

Page 8: C++ Session 3

Binary Arithmetic Operators (3)

Subtracts the second operand from first

answer =

The subtraction operator

Page 9: C++ Session 3

Multiplies the operands

ans =

The multiplication operator

Binary Arithmetic Operators (4)

Page 10: C++ Session 3

Divides the first operand by the second

The division operator

result =

Binary Arithmetic Operators (5)

Page 11: C++ Session 3

Store the remainder after integer division

The modulus operator

answer =

Binary Arithmetic Operators (6)

Page 12: C++ Session 3

void main(void){ :cout << endl ;f_dist = f_speed * f_time ;cout<<"The distance covered by the vehicle = " ; cout<< f_dist ;cout << endl ;} Multiplication

operator

Binary Arithmetic Operators (7)

Page 13: C++ Session 3

Unary arithmetic operators (1)

Require one operand

• Negation operator

-

• Increment operator

++

• Decrement operator

- -

int x; int x= -3

stnd ++ OR ++stnd

puls - - OR - - puls

Page 14: C++ Session 3

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

Unary arithmetic operators (2)

Page 15: C++ Session 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 ;}

Unary arithmetic operators (3)

Page 16: C++ Session 3

Assignment operator

Assignment operator

Page 17: C++ Session 3

Multiple assignmentint var_1, var_2, var_3 ;

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

var_1 = var_2 = var_3 = 70 ;

int var_1 = int var_2 = int var_3 ;

Page 18: C++ Session 3

Relational operators (1)

- Comparative action on data

- test relationship between variables

Page 19: C++ Session 3

Relational operators (2)

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

Page 20: C++ Session 3

Relational operators (3)

b == 3b== c

Equal to

Greater thanb > 3b > c

Less thanb < 3b < c

Less than Equal to

b <= 3b <= c

Greater than Equal tob >=

3b>= cNot equal

tob!= 3b!= c

Page 21: C++ Session 3

Logical operators (1)

Page 22: C++ Session 3

Logical operators (2)

qual == 12 && age == 18

qual == 10 || age == 18

( ! ( age >= 18) )

Page 23: C++ Session 3

Precedence and Order of Evaluation

Page 24: C++ Session 3

Mixed mode Expressions & Implicit Type conversion

X = 5 * 6.7 ;

integer

float

No error

Integer converted to double automatically

Page 25: C++ Session 3

Conversion table

Page 26: C++ Session 3

Type conversion (1)

Floating pointvariables

Integer values

Value is not changed

Page 27: C++ Session 3

Type conversion (2)

Floating pointvariables

Integer values

Value is changed

Page 28: C++ Session 3

Adding integers to characters

Page 29: C++ Session 3

Type casting (1)

Use explicit type conversionsin mixed mode expressionsfloat

a ;(int) a ;

Often leads to

Page 30: C++ Session 3

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

Page 31: C++ Session 3

C++ Shorthand Operators (1)Help in reducing the code required in programming constructs

test_val = test_val + 5;

test_val += 5;

Page 32: C++ Session 3

C++ Shorthand Operators (2)

Every shorthand operator

consists ofAn arithmetic

operator

+, -, /, *, %

Page 33: C++ Session 3

C++ Shorthand Operators (3)