on Leave a Comment

C++ Operators - Types of Operators

An operator is a symbol that performs some mathematical or logical operation on one or more variables. C++ has some built-in operators. But these operators only works on built-in data type variables. If you tried to add two objects with + operator, then compiler will show an error. An operator can be unary operator, binary operator or ternary operator.

C++ operators are:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Increment and Decrement Operators

Arithmetic Operators

Arithmetic operators are also known as mathematical operator. Arithmetic operators are: addition (+), subtraction (-), multiplication (*), division (/) and modular division (%). These operators are used to perform mathematical operations on variables.

This table shows the arithmetic operators with their use and example.

Arithmetic operators in C++


Relational Operators

Relational operators are used for comparing two values. Result of relational operator can be either 0 or 1. 0 shows false and 1 shows true.

The following table shows the relational operators with their meaning and example.

Relational operators in C++

Logical Operators

Logical operators are used to perform logical operations. Logical operators are AND (&&), OR (||), NOT (!).

Logical AND (&&)

If both statements are true, then result is true, otherwise false.

Logical OR (||)

If any statement is true, then result is true. If both statements are false, then result is false.

Logical NOT (!)

If statement is true, then result is false. If statement is false, then result is true.

The following table shows the logical operators and their meaning.

Logical operators in c++

Bitwise Operators

Bitwise operators are used to change the individual bits of a number. Binary bits are : 0 and 1. Bitwise operators only works on integral data types such as int, char and.

The following table shows the bitwise operators and their meaning.

Bitwise operators in C++

Assignment Operators

The following table shows the assignment operators and their example.

Assignment operators in C++


Increment and Decrement Operators

Increment operators increments the value of a variable by 1. Increment operators are of two types: pre increment  operator and post increment operators.

Pre increment operator

Example:

int a=1;
cout<<++a;

Output will be 2.

Post increment operator

Example:

int a=1;
cout<<a++;

Output will be 1.

Decrement operators decrements the value of a variable by 1. Decrement operators are of two types: pre decrement  operator and post decrement operators.

Pre decrement operator

Example:

int a=1;
cout<<--a;

Output will be 0.

Post decrement operator

Example:

int a=1;
cout<<a--;

Output will be 1.

The following table shows the increment and decrement operators and their meaning.

increment and decrement operators in c++


0 comments:

Post a Comment