c++4(Operators)

C++ Operators

All C language operator are valid in c++

  1. Arithmatic Operator(+,-,*,/,%)
  2. Relational Operator(<,<=,>,>=,==,!=)
  3. Logical Operator(&&,||,!)
  4. Assingment Operator(+=, -=, *=, /=)
  5. Increment and Decrement Operators(++,--)
  6. Conditional Operator(?:)
  7. Bitwise Operator(&,|,^,<<,>>)
  8. special Operatora()

Arthmetic Operator

Operators Example Meaning
+ a+b Addition
- a-b Subtraction
* a*b Multiplaction
/ a/b Division
% a%b Modulas(Division remainder)


Relational Operator

Operators Meaning
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== Equal to
!= Not Equal to


Logical Operator

Operators Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Example of conditions
a b a&&b a||b
true true true true
true false false true
false true false true
false false false false

a&&b: return false if any of the expression is false

a||b: return true if any of the expression is true

Assingment Operator

We assign a value to a variable using the basic assingment operator(=)
Assingment operators stores a value in memory.
left side(always it is a variable identifier) = right side(it is either a literal/ a variable identifiers/ an expression) literal : ex i=1;
variable identifers: ex. start = i;
Expression : ex.sum= first + second;

Assingment Operators(Short hand)

Syntax

leftside op = rightside;
Ex:

x=X+3;
X+=3;

Simple Assingment Operator Shorthand Operator
a=a+1 a+=1
a=a-1 a-=1
a=a*(m+n) a*=m+n
a=a/(m+n) a/=m+n
a=a%b a%=b


Increment and Decrement Operators

Increment ++

The ++ Operator used to increase he value of the variable by one.

Eg -
x=100;
x++;
After the execution the value of x will be 101.

Decrement --

The -- Operator used to decrease the value of the variable by one.

Eg -
x=100;
x--;
After the execution the value of x will be 99.

Pre & Post Increment Operator

Operator Discription
Pre-increment Operator(++x) value of x is incremented before assigning it to variable on the left
x=10;
p=++x;
after exection
x will be 11
p will be 11
Operator Discription
Post-increment operator(x++) Values of x is incremented after assigning it to the variables on the left
x=10;
p=x++;
after execution
x will be 11
p will be 10

Pre & Post Decrement Operator

Operator Discription
Post-decrement operator(--x) Values of x is decremented before assigning it to the variables on the left
x=10;
p=--x;
after execution
x will be 9
p will be 9
Operator Discription
Post-decrement operator(x++) Values of x is decremented after assigning it to the variables on the left
x=10;
p=x--;
after execution
x will be 9
pwill be 10