Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Compiler Tools

C++ Operators — Arithmetic, Logical, Bitwise

Types of Operators

CategoryOperators
Arithmetic+ - * / % ++ --
Relational== != < > <= >=
Logical&& || !
Assignment= += -= *= /= %= &= |= ^= <<= >>=
Bitwise& | ^ ~ << >>
Ternarycondition ? val1 : val2
sizeofsizeof(type)
Scope resolution::
Member access. ->
Arithmetic and Assignment Operators
#include <iostream>
using namespace std;

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

    // Arithmetic
    cout << "a + b = " << (a + b) << endl;  // 13
    cout << "a - b = " << (a - b) << endl;  // 7
    cout << "a * b = " << (a * b) << endl;  // 30
    cout << "a / b = " << (a / b) << endl;  // 3 (integer division)
    cout << "a % b = " << (a % b) << endl;  // 1 (remainder)

    // Increment / Decrement
    int x = 5;
    cout << "x++: " << x++ << endl;  // 5 (post: use then increment)
    cout << "x:   " << x   << endl;  // 6
    cout << "++x: " << ++x << endl;  // 7 (pre: increment then use)

    // Assignment operators
    int n = 10;
    n += 5;  cout << "n += 5: " << n << endl;  // 15
    n -= 3;  cout << "n -= 3: " << n << endl;  // 12
    n *= 2;  cout << "n *= 2: " << n << endl;  // 24
    n /= 4;  cout << "n /= 4: " << n << endl;  // 6
    n %= 4;  cout << "n %= 4: " << n << endl;  // 2

    return 0;
}
Relational, Logical and Ternary Operators
#include <iostream>
using namespace std;

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

    // Relational - return bool
    cout << boolalpha;
    cout << "a == b: " << (a == b) << endl;  // false
    cout << "a != b: " << (a != b) << endl;  // true
    cout << "a <  b: " << (a <  b) << endl;  // true
    cout << "a >= b: " << (a >= b) << endl;  // false

    // Logical
    bool x = true, y = false;
    cout << "x && y: " << (x && y) << endl;  // false (AND)
    cout << "x || y: " << (x || y) << endl;  // true  (OR)
    cout << "!x:     " << (!x)     << endl;  // false (NOT)

    // Ternary: condition ? if_true : if_false
    int age = 20;
    string status = (age >= 18) ? "Adult" : "Minor";
    cout << "Status: " << status << endl;  // Adult

    // Spaceship operator (C++20) - three-way comparison
    // auto result = a <=> b;  // negative if a < b

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.