C++ Operators

Welcome to The Coding College! Operators in C++ are fundamental tools that perform operations on variables and values. This tutorial explains different types of operators in C++, provides examples for each, and includes best practices to enhance your coding skills.

What Are Operators in C++?

Operators are symbols or keywords used to perform operations on data. For instance, the + operator adds two numbers, and the = operator assigns a value to a variable.

Types of Operators in C++

C++ supports the following categories of operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Miscellaneous Operators

1. Arithmetic Operators

These operators perform basic arithmetic operations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22 (integer division)
%Modulus (remainder)5 % 21

Example: Arithmetic Operators in Action

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;

    cout << "Addition: " << a + b << endl;
    cout << "Subtraction: " << a - b << endl;
    cout << "Multiplication: " << a * b << endl;
    cout << "Division: " << a / b << endl;
    cout << "Modulus: " << a % b << endl;

    return 0;
}

Output:

Addition: 13  
Subtraction: 7  
Multiplication: 30  
Division: 3  
Modulus: 1  

2. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExampleEquivalent To
=Assignx = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 2x = x * 2
/=Divide and assignx /= 2x = x / 2
%=Modulus and assignx %= 3x = x % 3

Example: Assignment Operators

#include <iostream>
using namespace std;

int main() {
    int x = 10;

    x += 5;  // x = x + 5
    cout << "After += : " << x << endl;

    x *= 2;  // x = x * 2
    cout << "After *= : " << x << endl;

    return 0;
}

Output:

After += : 15  
After *= : 30  

3. Comparison Operators

Comparison operators compare two values and return a Boolean (true or false).

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 3false

Example: Comparison Operators

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    cout << "a == b: " << (a == b) << endl;
    cout << "a > b: " << (a > b) << endl;

    return 0;
}

Output:

a == b: 0  
a > b: 1  

4. Logical Operators

Logical operators are used in conditions.

OperatorDescriptionExampleResult
&&Logical AND(5 > 3) && (5 > 2)true
``Logical OR
!Logical NOT!(5 == 3)true

Example: Logical Operators

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    cout << "Logical AND: " << ((a > b) && (b > 0)) << endl;
    cout << "Logical OR: " << ((a > b) || (b < 0)) << endl;
    cout << "Logical NOT: " << !(a == b) << endl;

    return 0;
}

Output:

Logical AND: 1  
Logical OR: 1  
Logical NOT: 1  

5. Bitwise Operators

These operate on binary representations of numbers.

OperatorDescriptionExample
&AND5 & 3
``OR
^XOR5 ^ 3
~NOT~5
<<Left shift5 << 1
>>Right shift5 >> 1

6. Miscellaneous Operators

OperatorDescription
sizeofReturns the size of a variable
? :Ternary operator for conditions
&Address-of operator
*Pointer dereference operator

Example: Ternary Operator

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    string result = (a > b) ? "a is greater" : "b is greater";
    cout << result << endl;

    return 0;
}

Output:

a is greater  

Best Practices for Using Operators

  1. Use Parentheses for Clarity: Parentheses make operations explicit and improve readability.
  2. Beware of Division by Zero: Always check for zero in denominators.
  3. Understand Precedence: Operators have precedence; for example, * is evaluated before +. Use parentheses to override precedence.
  4. Avoid Overcomplicated Expressions: Break down complex expressions for easier debugging.

Explore More at The Coding College

This guide covered the basics of C++ operators with practical examples. Dive deeper into C++ with more tutorials and coding challenges on The Coding College.

What’s Next?

  • Practice using operators in real-world scenarios.
  • Learn advanced concepts like operator overloading in C++.

Leave a Comment