DEV Community

Mehfila A Parkkulthil
Mehfila A Parkkulthil

Posted on

Day 5: C++ language | Arithmetic Operators

Topics to be covered

  • Operators
  • Types of operators
  • Arithmetic Operators

Operators

Operators are used to perform operations on variables and values.


Types of Operators

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. 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

addition

add output


Subtraction

sub out

sub output


Multiplication

multi

multi output


Division

division

division output


Modulus

modulus

modulus output


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)

Enter fullscreen mode Exit fullscreen mode

It is typically used in loops, conditional statements, and other control flow structures to manage variables.

Preincrement

prein output


Loop

Loop output


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.


Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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).

Enter fullscreen mode Exit fullscreen mode

Previous Blog

Top comments (0)