Geek Slack

Learn C++
About Lesson






C++ Operators


C++ Operators

Operators in C++ are symbols that perform operations on operands. They are used to manipulate data and variables in various ways. This chapter covers different types of operators available in C++.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on variables. They include:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder after division)

Example:

#include <iostream>

int main() {
    int a = 10, b = 3;
    int sum = a + b;
    int difference = a - b;
    int product = a * b;
    int division = a / b;
    int modulus = a % b;

    std::cout << "Sum: " << sum << std::endl;
    std::cout << "Difference: " << difference << std::endl;
    std::cout << "Product: " << product << std::endl;
    std::cout << "Division: " << division << std::endl;
    std::cout << "Modulus: " << modulus << std::endl;

    return 0;
}

2. Relational Operators

Relational operators are used to compare the values of two operands. They return either true (1) or false (0). Relational operators include:

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Example:

#include <iostream>

int main() {
    int x = 5, y = 10;
    bool isEqual = (x == y);
    bool isNotEqual = (x != y);
    bool isGreater = (x > y);
    bool isLess = (x < y);
    bool isGreaterOrEqual = (x >= y);
    bool isLessOrEqual = (x <= y);

    std::cout << "Is Equal: " << isEqual << std::endl;
    std::cout << "Is Not Equal: " << isNotEqual << std::endl;
    std::cout << "Is Greater: " << isGreater << std::endl;
    std::cout << "Is Less: " << isLess << std::endl;
    std::cout << "Is Greater Or Equal: " << isGreaterOrEqual << std::endl;
    std::cout << "Is Less Or Equal: " << isLessOrEqual << std::endl;

    return 0;
}

3. Logical Operators

Logical operators are used to perform logical operations on boolean values. They include:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Example:

#include <iostream>

int main() {
    bool condition1 = true, condition2 = false;
    bool resultAND = (condition1 && condition2);
    bool resultOR = (condition1 || condition2);
    bool resultNOT = !condition1;

    std::cout << "Result of AND: " << resultAND << std::endl;
    std::cout << "Result of OR: " << resultOR << std::endl;
    std::cout << "Result of NOT: " << resultNOT << std::endl;

    return 0;
}

4. Assignment Operators

Assignment operators are used to assign values to variables. They include:

  • =: Assign
  • +=: Add and assign
  • -=: Subtract and assign
  • *=: Multiply and assign
  • /=: Divide and assign
  • %=: Modulus and assign

Example:

#include <iostream>

int main() {
    int num = 10;
    num += 5; // Equivalent to num = num + 5
    std::cout << "Num after += operation: " << num << std::endl;

    return 0;
}

5. Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1, respectively.

#include <iostream>

int main() {
    int count = 5;
    std::cout << "Initial count: " << count << std::endl;
    count++; // Increment
    std::cout << "After increment: " << count << std::endl;
    count--; // Decrement
    std::cout << "After decrement: " << count << std::endl;

    return 0;
}

Join the conversation