Select Page

## Introduction

Operators are a fundamental part of any programming language. They are used to perform various mathematical, logical, or other operations on one or more operands. In this tutorial, we will cover the primary operators in C++.

## Operators in c++

### What are operators in c++?

Operators in C++ are symbols that we use to tell the compiler what type of operation to perform. They are symbols that are used to manipulate values.Operators are symbols that we use to manipulate numbers in c++. There are two types: unary and binary. Unary operators require only one operand, and binary operators require two operands.

# We will be taking a watch at these operators:

Arithmetic Operators     Relational Operators Logical Operators        Assignment Operators                       Increment/Decrement Operators             Member access Operators

### Assignment Operators

Assignment Operators are a way of assigning values to variables. Assignment Operators are used in programming languages to assign a value to a variable.

Variables are containers that hold data and can be assigned values by the user. The data can be anything from letters, numbers, and text strings. Variables can be assigned any value typed into the programmer’s code.Some common assignment operators are:

• = (equals)
• += (plus equals)
• -= (minus equals)
• *= (times equals)
• /= (divides equals)

#### = (equals)

In C++, the equals sign (=) is used to assign a value to a variable.The equals sign can be used to assign the result of an arithmetic expression, as in:int x = 5 + 4; // assigns the value 9 to variable xint y = 5 * 4; // assigns the value 20 to variable y

#### += (plus equals)

The line below shows how you can use += in c++:+=a += b;a = a + b;

#### -= (minus equals)

In c++, the minus and equals signs are used to perform arithmetic operations.The following lines shows how you can use -= in c++:int x = 10;x -= 5; // x now contains 5x = x + 4; // x now contains 9

#### *= (times equals)

The line below shows how you can use *= in c++:*=x *= y;x = x * y;

#### /= (divides equals)

The line below shows how you can use /= in c++:/=x /= y;x = x / y;

#### %= (modulo equals)

The line below shows how you can use %= in c++:%=x %= y;x = x % y;

### Arithmetic operator

An arithmetic operator is a symbol or set of symbols that let us perform the mathematical operation of addition, subtraction, multiplication, division, or exponentiation.

• Subtraction: –
• Multiplication: *
• Division: /
• Exponentiation: ^

### Increment and decrement operators in c++

The increment operator (++) is a unary operator that adds one to its operand.The decrement operator (–) is a unary operator that subtracts one from its operand.These operators are often used in loops, where the operand is either a variable or an expression.

The increment and decrement operators are used to change the value of a variable by a given number.

The increment operator is written as ++. It means that the variable should be increased by one. The decrement operator is written as — meaning that the variable should be decreased by one.

### Mixed expressions

Mixed expressions can be converted to the equivalent expressions with only unary or only binary operators, but not both.

For example:

x + y * z -> x + (y * z) x – y * z -> x – (y * z) x / y * z -> x / (y * z) A mixed expression can be converted to the equivalent expression with only unary or only binary operators, but not both.

### Testing for equality c++

The == operator compares two operands to see if they are equal.If the two operands are of the exact type, they are equal if they have the same value.

The == operator is an equivalence relation that satisfies the following properties: reflexive, symmetric, and transitive.A common mistake is to use the = operator instead of the == operator when testing for equality in c++.

For example:

5 == 5 is true and “hello” == “hello” is true.The purpose of the == operator is to compare the operands for equality. If the operands are equal, it returns a true Boolean value.

The purpose of the != operator is to compare the operands for inequality. If the operands are not equal, then it returns a Boolean value of true.

### Relational operators in c++

The relational operators are used to compare two operands and return a boolean value.

### Relational operators in c++

> (greater than)< (less than)>= (greater than or equal to)<= (less than or equal to).<=> (spaceship three-way comparison (C++20).

### Logical operators

The logical operators in C++ are the following:

• The logical NOT operator is !
• The logical AND operator is &&
• The logical OR operator is ||

Logical NOT, AND, and OR are binary operators that work on two operands. The operation of these three operators can be summarized as follows:

• NOT(A) = !A;
• AND(A AND B) = A && B;
• OR(A OR B) = A || B;

### Member access operators

The member access operator is a special type of operator that is used to access members of a class. This can be done with the arrow operator (->)

### Operator precedence c++

Operator precedence is a collection of rules determining the order in which an expression should be evaluated. These rules are applied to all expressions in a statement, including mathematical expressions.

For example:

When evaluating the expression 3+4*5, one should first evaluate the multiplication (*) and then the addition (+). The result of this evaluation is 23.When evaluating the expression 3+4-5, one should first evaluate the multiplication (+) and then the addition (-). The result of this evaluation is 2.

## Conclusion

Operators in c++ are used to perform Arithmetic operators, increment and decrement operators, mixed expressions Relational operators, and logical operators. Operators have Operator precedence or order in which an expression should be evaluated.This is important to make the right C++ program needed. Operators are the backbone of C++. They allow the programmer to control program flow.