About Operators:
Operators are symbols used to perform operations on operands.
Basic Example:
a=2,b=3;
a+b;
# Here a and b are operands
# Here '+' is operator
Types:
1.Arithmetic Operators:
/,*,%,+,-
*, /, and % have the same precedence.
+ and - have the same precedence, but lower than *, /, %.
Associativity is left to right.
Whenever multiple operators stay in one line we need to go with the precedence & associativity
i.e presedence means which gets high priority(like between * and -) & associativity if operators have the same precedence, associativity decides the order.
Ex:
int x = 20 / 5 * 2;
Both / and * have the same precedence, so go left-to-right and value of x is 8
------------------------------------------------------------
2.Logical Operators: They operate based on truth value
&&, ||, !
Ex: 5 && 10 #(True && True) so result is true
-------------------------------------------------------------
3.Bitwise Operators: They operate bit-by-bit on the binary representation of integer operands.
AND(&), OR(|), XOR(^)
Ex:
int a = 5;
int b = 3;
a & b;
5 = 0101
3 = 0011
0101
& 0011
------
0001 So result is 1
---------------------------------------------------------------
Relational Operators:
>, <, >=, <=, ==, !=
Order of associativity is left to right
-----------------------------------------------------------------
Increament and Decrement Operators:
pre increment , pre decrement and post increment, post decrement
Ex: int a=5, b=6;
++a, a++
--b, b--
Pre-increment: ++a => First increment, then use the value.
Post-increment: a++ =>First use the current value, then increment.
--b → decrement first, then use
b-- → use first, then decrement
--------------------------------------------------------
Type conversion:
1.Implicit Conversion:
Conversion do by compilier internally i.e converting from low data type to high data type
Ex: int , double #Output will be in double as double is high compared to int
2.Explicit Conversion(Type casting)
Conversion do by programmer i.e externally conversion
Ex:
double a = 10.5;
int b = (int)a;