Operators in C++

Operators play a crucial role in C++ programming, allowing you to perform various operations on data, manipulate values, make comparisons, and control program flow. Understanding the different types of operators and their usage is essential for writing effective and efficient code. In this tutorial, we'll explore the most commonly used operators in C++ along with examples.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations on numeric data types. Here are the main arithmetic operators in C++:

  • Addition (+): Adds two values together.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies two values.
  • Division (/): Divides one value by another.
  • Modulus (%): Computes the remainder of a division operation.

Let's see some examples:

#include<iostream>
using namespace std;
int main() 
{
    // Arithmetic Operators
    int a = 5;
    int b = 3;
    int result;
    result = a + b;      // Addition: 5 + 3 = 8
    cout << "Addition: " << result << endl;
    result = a - b;      // Subtraction: 5 - 3 = 2
    cout << "Subtraction: " << result << endl;
    result = a * b;      // Multiplication: 5 * 3 = 15
    cout << "Multiplication: " << result << endl;
    result = a / b;      // Division: 5 / 3 = 1 (integer division)
    cout << "Division: " << result << endl;
    result = a % b;      // Modulus: 5 % 3 = 2 (remainder of division)
    cout << "Modulus: " << result << endl;
    return 0;
}

Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is the equals sign (=), which assigns the value on the right side to the variable on the left side.

int x = 5;
int y;
y = x;      // Assigns the value of x (5) to y

C++ also provides compound assignment operators, which combine an arithmetic operation with assignment:

Addition and assignment (+=)

It adds the value on the right-hand side of the operator to the variable on the left-hand side and assigns the result back to the variable.

Subtraction and assignment (-=)

It subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result back to the variable.

Multiplication and assignment (*=)

It multiplies the value on the right-hand side of the operator with the variable on the left-hand side and assigns the result back to the variable.

Division and assignment (/=)

It divides the variable on the left-hand side by the value on the right-hand side of the operator and assigns the result back to the variable.

Modulus and assignment (%=)

It calculates the remainder of dividing the variable on the left-hand side by the value on the right-hand side of the operator and assigns the result back to the variable.

Here's an example:

#include<iostream>
using namespace std;
int main() 
{
    // Assignment Operators
    int x = 5;
    int y;
    y = x;      // Assigns the value of x (5) to y
    cout << "Assigned value: " << y << endl;
    int c = 5;
    int d = 3;
    c += d;      // Equivalent to c = c + d;  (c = 5 + 3; c = 8)
    cout << "Compound Assignment: " << c << endl;
    return 0;
}

Comparison Operators

Comparison operators are used to compare values and determine the truth of a condition. They return a boolean value (true or false) based on the comparison result. Here are the comparison operators in C++:

Equal to (==)

The "equal to" operator compares two values and returns true if they are equal, and false otherwise. It does not modify the values being compared.

Not equal to (!=)

The "not equal to" operator compares two values and returns true if they are not equal, and false if they are equal. It does not modify the values being compared.

Greater than (>)

The "greater than" operator compares two values and returns true if the value on the left is greater than the value on the right, and false otherwise. It does not modify the values being compared.

Less than (<)

The "less than" operator compares two values and returns true if the value on the left is less than the value on the right, and false otherwise. It does not modify the values being compared.

Greater than or equal to (>=)

The "greater than or equal to" operator compares two values and returns true if the value on the left is greater than or equal to the value on the right, and false otherwise. It does not modify the values being compared.

Less than or equal to (<=)

The "less than or equal to" operator compares two values and returns true if the value on the left is less than or equal to the value on the right, and false otherwise. It does not modify the values being compared.

#include<iostream>
using namespace std;
int main() 
{
    // Comparison Operators
    int e = 5;
    int f = 3;
    bool result1;
    result1 = e == f;      // Equal to: false
    cout << "Equal to: " << result1 << endl;
    result1 = e != f;      // Not equal to: true
    cout << "Not equal to: " << result1 << endl;
    result1 = e > f;       // Greater than: true
    cout << "Greater than: " << result1 << endl;
    result1 = e < f;       // Less than: false
    cout << "Less than: " << result1 << endl;
    result1 = e >= f;      // Greater than or equal to: true
    cout << "Greater than or equal to: " << result1 << endl;
    result1 = e <= f;      // Less than or equal to: false
    cout << "Less than or equal to: " << result1 << endl;
    return 0;
}

Logical Operators

Logical operators are used to combine or negate conditions. They operate on boolean values and return a boolean result. The three main logical operators in C++ are:

  • Logical AND (&&): Returns true if both conditions are true.
  • Logical OR (||): Returns true if either of the conditions is true.
  • Logical NOT (!): Negates the result, flipping true to false and false to true.
#include<iostream>
using namespace std;
int main() 
{
    //Logical Operators
    bool condition1 = true;
    bool condition2 = false;
    bool result2;
    result2 = condition1 && condition2;    // Logical AND: false
    cout << "Logical AND: " << result2 << endl;
    result2 = condition1 || condition2;    // Logical OR: true
    cout << "Logical OR: " << result2 << endl;
    result2 = !condition1;                 // Logical NOT: false
    cout << "Logical NOT: " << result2 << endl;
    return 0;
}

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1, respectively. They can be used both as prefix or postfix operators.

#include<iostream>
using namespace std;
int main() {
    // Increment and Decrement Operators
    int g = 5;
    g++;        // Postfix increment: g = 6
    cout << "Postfix Increment: " << g << endl;
    ++g;        // Prefix increment: g = 7
    cout << "Prefix Increment: " << g << endl;
    g--;        // Postfix decrement: g = 6
    cout << "Postfix Decrement: " << g << endl;
    --g;        // Prefix decrement: g = 5
    cout << "Prefix Decrement: " << g << endl;
    return 0;
}

Other Operators

C++ provides various other operators, including:

Ternary Operator (?:)

The ternary operator, also known as the conditional operator, is a concise way to write a conditional expression in many programming languages. It takes three operands and evaluates a condition, returning one of two values based on the result of the condition.

condition ? value_if_true : value_if_false

Sizeof Operator

sizeof operator is used to determine the size, in bytes, of a data type or a variable. It provides a convenient way to obtain the memory footprint of an object or the data type itself.

Member Access Operators (., ->)

Used to access members of a class or structure.