c++ session 3
TRANSCRIPT
![Page 1: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/1.jpg)
Session 3
![Page 2: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/2.jpg)
Session objectives (1)• Identify operators
• Assignment• Arithmetic• Compound • Assignment• Relational• Logical • Bitwise
•Understand precedence and evaluation
of operators
![Page 3: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/3.jpg)
•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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/4.jpg)
IntroductionOperators
are: • Are a set of characters
• Have specific meaning particular to a
language • Do not include special characters ; “ : ,
![Page 5: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/5.jpg)
Operands
![Page 6: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/6.jpg)
Binary Arithmetic Operators (1)
Require two operands
![Page 7: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/7.jpg)
Binary Arithmetic Operators (2)
Adds the values of operands
result =
The addition operator
![Page 8: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/8.jpg)
Binary Arithmetic Operators (3)
Subtracts the second operand from first
answer =
The subtraction operator
![Page 9: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/9.jpg)
Multiplies the operands
ans =
The multiplication operator
Binary Arithmetic Operators (4)
![Page 10: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/10.jpg)
Divides the first operand by the second
The division operator
result =
Binary Arithmetic Operators (5)
![Page 11: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/11.jpg)
Store the remainder after integer division
The modulus operator
answer =
Binary Arithmetic Operators (6)
![Page 12: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/14.jpg)
#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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/16.jpg)
Assignment operator
Assignment operator
![Page 17: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/18.jpg)
Relational operators (1)
- Comparative action on data
- test relationship between variables
![Page 19: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/21.jpg)
Logical operators (1)
![Page 22: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/22.jpg)
Logical operators (2)
qual == 12 && age == 18
qual == 10 || age == 18
( ! ( age >= 18) )
![Page 23: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/23.jpg)
Precedence and Order of Evaluation
![Page 24: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/24.jpg)
Mixed mode Expressions & Implicit Type conversion
X = 5 * 6.7 ;
integer
float
No error
Integer converted to double automatically
![Page 25: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/25.jpg)
Conversion table
![Page 26: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/26.jpg)
Type conversion (1)
Floating pointvariables
Integer values
Value is not changed
![Page 27: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/27.jpg)
Type conversion (2)
Floating pointvariables
Integer values
Value is changed
![Page 28: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/28.jpg)
Adding integers to characters
![Page 29: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/29.jpg)
Type casting (1)
Use explicit type conversionsin mixed mode expressionsfloat
a ;(int) a ;
Often leads to
![Page 30: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/32.jpg)
C++ Shorthand Operators (2)
Every shorthand operator
consists ofAn arithmetic
operator
+, -, /, *, %
![Page 33: C++ Session 3](https://reader035.vdocuments.us/reader035/viewer/2022081511/555e3705d8b42aad058b4920/html5/thumbnails/33.jpg)
C++ Shorthand Operators (3)