Topics to be covered
- Operators
- Types of operators
- Arithmetic Operators
Operators
Operators are used to perform operations on variables and values.
Types of Operators
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Let two variable x and y.
- Addition
x + y
- Subtraction
x - y
- Multiplication
x * y
- Division
x / y
- Modulus (Returns the division remainder)
x % y
- Increment Increases the value of a variable by 1
++x
- Decrement Decreases the value of a variable by 1
--x
Addition
Subtraction
Multiplication
Division
Modulus
Increment
Increment operators can be divided into 2 :
- Pre-increment operator
++x
- Post-increment operator
x++
Pre-increment operator
The pre-increment operator (++x)
is an operator in C++ that increases the value of a variable by 1 before its value is used in an expression.
Example:
int x = 5;
int y = ++x; // x is increased to 6, then y gets the value 6
In this example:
x is increased by 1, so x becomes 6
y is then assigned the value of x (which is now 6)
It is typically used in loops, conditional statements, and other control flow structures to manage variables.
Post-increment operator
The post-increment operator (x++)
is an operator in C++ that increases the value of a variable by one, but unlike the pre-increment operator, the increment occurs after the current value of the variable is used in an expression.
Example:
int x = 5;
int y = x++; // y gets the value 5, then x is increased to 6.
In this example:
y is assigned the value of x (which is 5).
After the assignment, x is increased by 1, so x becomes 6.
It is typically used in loops, conditional statements, and other control flow structures to manage variables.
Decrement
Decrement operators decrease the value of a variable by 1. There are two types:
Post-decrement (variable--)
Pre-decrement (--variable)
Post-decrement (x--)
:
The post-decrement operator decreases the value of the variable after the expression is evaluated.
Example:
int x = 5;
int y = x--; // y gets the value 5, then x is decremented to 4
In this example:
y is assigned the value of x (which is 5).
After the assignment, x is decremented by 1, so x becomes 4.
Pre-decrement (--x)
The pre-decrement operator decreases the value of the variable before the expression is evaluated.
Example:
int x = 5;
int y = --x; // x is decremented to 4, then y gets the value 4
In this example:
x is decremented by 1, so x becomes 4.
y is then assigned the value of x (which is now 4).
Top comments (0)