In this C++ tutorial, you will learn about operators, the assignment operator, arithmetic operators, compound assignment operators, increment and decrement operator, the functionality of prefix and postfix operators, relational and equality operators.
The operators available in C++ programming language are:
- Assignment Operator denoted by =
- Arithmetic operators denoted by +, -, *, /, %
- Compound assignment Operators denoted by +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
- Increment and Decrement operator denoted by ++, —
- Relational and equality operators denoted by ==, !=, >, <, >=, <=
- Logical operators denoted by !, &&, ||
- Conditional operator denoted by ?
- Comma operator denoted by ,
- Bitwise Operators denoted by &, |, ^, ~, <<, >>
- Explicit type casting operator
- sizeof()
Assignment Operator
This is denoted by symbol “=”. This operator is used for assigning a value to a variable. The left of the assignation operator is known as the lvalue (left value), which must be a variable. The right of the assignation operator is known as the rvalue (right value). The rvalue can be a constant, a variable, the result of an operation or any combination of these.
For example:
x = 5;
By following the right to left rule the value 5 is assigned to the variable x in the above assignment statement.
Arithmetic operators
The operators used for arithmetic operations in C++ are:
- + For addition
- – For subtraction
- * For multiplication
- / For division
- % For modulo
Compound assignment Operators
This operator is used when a programmer wants to update a current value by performing operation on the current value of the variable.
Old += new is the same as Old = old + new
Compound assignment operators function in a similar way the other operators +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= function.
Increment and Decrement Operator
The increment operator is denoted by ++ and the decrement operator by –. The function of the increment operator is to increase the value by 1 and the decrement operator is to decrease the value by 1. These operators may be used as either prefix or postfix. A Prefix operator is written before the variable as ++a or –a. A Postfix operator is written after the variable as a++ or a–.
The Functionality of Prefix and Postfix Operators
In the case that the increment or decrement operator is used as a prefix ( ++a or –a), then the value is respectively increased or decreased by 1 before the result of the expression is evaluated. Therefore, the increased or decreased value, respectively, is considered in the outer expression. In the case that the increment or decrement operator is used as a postfix (a++ or a–), then the value stored in a is respectively increased or decreased after being evaluated. Therefore, the value stored before the increase or decrease operation is evaluated in the outer expression.
For example:
y=3;
x=++y; //Prefix : Here Value of x becomes 4
But for the postfix operator namely as below:
y=3 //Postfix : Here Value of x is 3 and Value of y is 4
x=y++;
Relational and Equality Operators
These operators are used for evaluating a comparison between two expressions. The value returned by the relational operation is a Boolean value (true or false value). The operators used for this purpose in C++ are:
- == Equal to
- != Not equal to
- > Greater than
- < Less than
- >= Greater than or equal to
- <= Less than or equal to