c++ programming unit 4 operators
TRANSCRIPT
![Page 1: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/1.jpg)
C++ LanguageBy:-AAKASH KAUSHIK#9289817971, [email protected]
![Page 2: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/2.jpg)
UNIT -4OPERATORS & EXPRESSIONS
![Page 3: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/3.jpg)
OPERATORSAn operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. operators operates on some data to give results
For ex :- A+B=CHere A, B are operands and + is the
operator which produces C as a result of addition of A and B.
AAKASH KAUSHIK 9891983083,9289817971
![Page 4: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/4.jpg)
Based on the no. of operand needed for operation operators can be broadly classified into 3 categories.UNARY OPERATORSBINARY OPERATORSTERNARY OPERATORS
CLASSIFICATION OF OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 5: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/5.jpg)
UNARY OPERATORSThe unary operators operate
on a single operand
![Page 6: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/6.jpg)
UNARY (–) MINUS – used to represent –ive values.
ex.:- -5,-50,-3.14 etc.
UNARY (+) PLUS – used to represent +ive values.
ex.:- +5,+50,+3.14 etc.
In C++ by default values are considered as +ive so no need to explicitly use UNARY (+)PLUS.
UNARY PLUS(+) & UNARY MINUS(-)
AAKASH KAUSHIK 9891983083,9289817971
![Page 7: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/7.jpg)
Increment operator increases integer value by one.For ex.:-int a=10;a++; //will give 11
Decrement operator decreases integer value by one.For ex.:-int a=10;a--; //will give 9
INCREMENT/DECREMENT OPERATOR
AAKASH KAUSHIK 9891983083,9289817971
![Page 8: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/8.jpg)
BINARY OPERATORSThe Binary operator takes 2
arguments.
![Page 9: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/9.jpg)
ARITHMETIC OPERATORSRELATIONAL OPERATORSLOGICAL OPERATORS
TYPE OF BINARY OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 10: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/10.jpg)
Arithmetic operators are used for mathematic calculations.
ARITHMETIC OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 11: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/11.jpg)
A relational operator compares two operands to determine whether one is greater than, greater than or equal to, less than, less than or equal to the other.
If the condition is true, it will return non-zero value, if the condition is false, it will return 0.
RELATIONAL OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 12: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/12.jpg)
RELATIONAL OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 13: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/13.jpg)
Logical operators are used in situation when we have more then one condition in a singlestatement.The logical operators && and || are used when evaluating two expressions to obtain a single relational result.The logical operator ! Is used for negation purpose. Basically, it returns the opposite Boolean value of evaluating its operand.
LOGICAL OPERATORS(AND,OR,NOT)
AAKASH KAUSHIK 9891983083,9289817971
![Page 14: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/14.jpg)
LOGICAL OPERATORS
AAKASH KAUSHIK 9891983083,9289817971
![Page 15: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/15.jpg)
TERNARY OPERATORSTHE ONE AND ONLY TERNARY OPERATOR IS CONDTIONAL
OPERATOR( ? : )
![Page 16: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/16.jpg)
The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. First is condition, second and third is value. The conditional operator check the condition, if condition is true, it will return second value, if condition is false, it will return third value.
Syntax:-val = condition ? val1 : val2;
CONDITIONAL OPERATOR
AAKASH KAUSHIK 9891983083,9289817971
![Page 17: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/17.jpg)
#include<iostream.h>#include<conio.h> void main() {clrscr(); int x=5,y=2,lrg;lrg = (x>y) ? x : y; cout<<"\nlargest number is : "<<lrg;getch();}Output : Largest number is : 5
TERNARY OPERATOR USAGE EXAMPLE
AAKASH KAUSHIK 9891983083,9289817971
![Page 18: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/18.jpg)
SOME MORE SPECIAL OPERATORS
SIZEOF & COMMA OPERATOR
![Page 19: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/19.jpg)
The sizeof is a compile time operator, and used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier.
m=sizeof(sum);n=sizeof(long int);
k=sizeof(235L);
SIZEOF OPERATOR
AAKASH KAUSHIK 9891983083,9289817971
![Page 20: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/20.jpg)
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.For example, the following code: a = (b=3, b+2);would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.
COMMA OPERATOR(,)
AAKASH KAUSHIK 9891983083,9289817971
![Page 21: c++ programming Unit 4 operators](https://reader036.vdocuments.site/reader036/viewer/2022062412/587a3f721a28abdb1c8b7ee9/html5/thumbnails/21.jpg)
THANK YOU
AAKASH KAUSHIK 9891983083,9289817971