Paramclasses Academy - Blog

Tutorials Details

What is Operators and Expressions in C++

What is Operators and Expressions in C++

October 15, 2026

3.1 Module overview

Operators are symbols that perform operations on values and variables. They allow C++ programs to calculate results, compare values, combine logical conditions, update variables and manipulate individual bits.

For example, in total = price * quantity;, the * operator multiplies two values, while = assigns the result to total.

Learning objectives

By the end of this module, students will be able to:

  • Understand operators, operands and expressions.

  • Use arithmetic, assignment and comparison operators.

  • Apply logical operators in conditions.

  • Understand increment and decrement operators.

  • Use compound assignment and the conditional operator.

  • Recognize bitwise operators.

  • Understand operator precedence and associativity.

  • Write programs using operators and expressions.

Prerequisites: Module 1 — Introduction to C++ and Module 2 — Variables, Data Types and Constants.

Estimated learning time: 3–4 hours, including practical exercises.

3.2 What are operators and operands?

An operator performs an operation on one or more operands. An operand is a value or expression on which the operator acts.

Example:

int result = 10 + 5;

In this statement:

  • 10 and 5 are operands.

  • + is the arithmetic operator.

  • 10 + 5 is an expression.

  • result stores the calculated value, 15.

C++ operators can be classified by the number of operands:

Type

Description

Example

Unary

Operates on one operand

-x

Binary

Operates on two operands

a + b

Ternary

Uses three operands

condition ? a : b

3.3 Arithmetic operators

Arithmetic operators perform mathematical calculations.

Operator

Operation

Example (a = 10, b = 3)

Result

+

Addition

a + b

13

-

Subtraction

a - b

7

*

Multiplication

a * b

30

/

Division

a / b

3

%

Remainder

a % b

1

For integer operands, / performs integer division and discards the fractional part. The % operator produces the remainder for integer operands.

Example: Arithmetic operations

#include <iostream>

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

    std::cout << "Addition: " << a + b << '\n';
    std::cout << "Subtraction: " << a - b << '\n';
    std::cout << "Multiplication: " << a * b << '\n';
    std::cout << "Integer division: " << a / b << '\n';
    std::cout << "Remainder: " << a % b << '\n';

    return 0;
}

Output

Addition: 13
Subtraction: 7
Multiplication: 30
Integer division: 3
Remainder: 1

Important: Integer vs floating-point division

int a = 10;
int b = 4;

std::cout << a / b << '\n';
std::cout << static_cast<double>(a) / b << '\n';

Output:

2
2.5

The first expression uses integer division. The second converts a to double before performing division.

Remember: Never divide by zero. Integer division or remainder with a zero divisor has undefined behaviour in C++.

3.4 Assignment operators

Assignment operators store values in variables.

Operator

Example

Equivalent operation

=

x = 10;

Assign 10 to x

+=

x += 5;

x = x + 5;

-=

x -= 2;

x = x - 2;

*=

x *= 3;

x = x * 3;

/=

x /= 2;

x = x / 2;

%=

x %= 3;

x = x % 3;

Example

#include <iostream>

int main() {
    int x = 10;

    x += 5;
    std::cout << x << '\n';

    x *= 2;
    std::cout << x << '\n';

    x -= 4;
    std::cout << x << '\n';

    return 0;
}

Output

15
30
26

Compound assignment operators are useful when updating counters, totals and accumulated values.

3.5 Relational operators

Relational operators compare two values. Their expressions produce a Boolean result, true or false.

Operator

Meaning

Example

==

Equal to

a == b

!=

Not equal to

a != b

>

Greater than

a > b

<

Less than

a < b

>=

Greater than or equal to

a >= b

<=

Less than or equal to

a <= b

Example

#include <iostream>

int main() {
    int a = 10;
    int b = 20;

    std::cout << std::boolalpha;

    std::cout << (a == b) << '\n';
    std::cout << (a != b) << '\n';
    std::cout << (a < b) << '\n';
    std::cout << (a >= b) << '\n';

    return 0;
}

Output

false
true
true
false

std::boolalpha tells the output stream to display Boolean values as true or false rather than 1 or 0.

Common mistake: = assigns a value; == compares two values.

3.6 Logical operators

Logical operators combine or reverse Boolean conditions.

| Operator | Name | Meaning | |---|---|---| | && | Logical AND | True when both operands are true | | || | Logical OR | True when at least one operand is true | | ! | Logical NOT | Reverses a Boolean value |

Truth table

| A | B | A && B | A || B | |---|---|---|---| | false | false | false | false | | false | true | false | true | | true | false | false | true | | true | true | true | true |

Example: Eligibility check

#include <iostream>

int main() {
    int age = 22;
    bool hasId = true;

    if (age >= 18 && hasId) {
        std::cout << "Eligible\n";
    } else {
        std::cout << "Not eligible\n";
    }

    return 0;
}

Output

Eligible

The condition is true because the age requirement is met and hasId is also true.

Short-circuit evaluation

C++ evaluates logical operators from left to right, with short-circuiting:

  • For A && B, if A is false, B is not evaluated.

  • For A || B, if A is true, B is not evaluated.

This can be useful when checking conditions safely, such as validating a pointer before accessing an object through it.

3.7 Increment and decrement operators

The increment operator ++ increases a modifiable value by one. The decrement operator -- decreases it by one.

Operator

Meaning

++x

Prefix increment

x++

Postfix increment

--x

Prefix decrement

x--

Postfix decrement

Prefix vs postfix

#include <iostream>

int main() {
    int x = 5;

    std::cout << ++x << '\n';

    x = 5;
    std::cout << x++ << '\n';
    std::cout << x << '\n';

    return 0;
}

Output

6
5
6

Explanation:

  • ++x increments x first, then produces the updated value.

  • x++ produces the old value, then increments x.

Use increment and decrement operations in simple, separate statements when possible. Avoid modifying the same variable multiple times in one complicated expression.

3.8 Bitwise operators

Bitwise operators work on the individual bits of integral operands.

| Operator | Name | Purpose | |---|---|---| | & | Bitwise AND | Sets a bit when both corresponding bits are set | | | | Bitwise OR | Sets a bit when either corresponding bit is set | | ^ | Bitwise XOR | Sets a bit when corresponding bits differ | | ~ | Bitwise NOT | Inverts the bits | | << | Left shift | Shifts bits to the left, subject to language rules | | >> | Right shift | Shifts bits to the right, subject to language rules |

Consider the binary representations of two positive integers:

5 = 0101
3 = 0011

Example

#include <iostream>

int main() {
    int a = 5;
    int b = 3;

    std::cout << (a & b) << '\n';
    std::cout << (a | b) << '\n';
    std::cout << (a ^ b) << '\n';

    return 0;
}

Output

1
7
6

For these values:

AND: 0101 & 0011 = 0001  → 1
OR:  0101 | 0011 = 0111  → 7
XOR: 0101 ^ 0011 = 0110  → 6

Bitwise operators are used in low-level programming, flags, hardware interfaces and other tasks involving bit representations.

Important distinction: & and | are bitwise operators for integral operands, while && and || are logical operators.

3.9 Conditional (ternary) operator

The conditional operator ?: chooses one of two expressions according to a condition.

Syntax

condition ? expression_if_true : expression_if_false;

Example

#include <iostream>

int main() {
    int age = 20;

    const char* status =
        (age >= 18) ? "Adult" : "Minor";

    std::cout << status << '\n';

    return 0;
}

Output

Adult

If age >= 18 is true, the first expression is selected; otherwise, the second is selected.

The conditional operator is useful for simple choices. For more complex decision-making, an if-else statement is generally clearer.

3.10 Operator precedence and associativity

When an expression contains multiple operators, precedence determines which operators bind more tightly, while associativity determines grouping when operators have the same precedence.

Example:

int result = 10 + 5 * 2;

Multiplication has higher precedence than addition, so the result is 20, not 30.

Parentheses can make the intended grouping explicit:

int result = (10 + 5) * 2;

Now the result is 30.

Common precedence order

This table shows selected operator groups, from higher to lower precedence.

| Operators | Category | |---|---| | () | Function call | | ++, -- (postfix) | Postfix increment/decrement | | ++, --, !, unary +, unary - | Unary operators | | *, /, % | Multiplicative | | +, - | Additive | | <<, >> | Shift | | <, <=, >, >= | Relational | | ==, != | Equality | | &, ^, | | Bitwise | | &&, || | Logical | | ?: | Conditional | | =, +=, -=, etc. | Assignment |

This is a selected introductory list rather than a complete precedence reference.

Best practice: Use parentheses in expressions when they make the intended calculation easier to understand, even if you already know the precedence rules.

3.11 Practical project: Simple calculator

This program accepts two numbers and an operator, then performs the selected calculation.

#include <iostream>

int main() {
    double a, b;
    char op;

    std::cout << "Enter an expression (e.g. 10 + 5): ";
    std::cin >> a >> op >> b;

    switch (op) {
        case '+':
            std::cout << "Result: " << a + b << '\n';
            break;

        case '-':
            std::cout << "Result: " << a - b << '\n';
            break;

        case '*':
            std::cout << "Result: " << a * b << '\n';
            break;

        case '/':
            if (b == 0) {
                std::cout << "Error: division by zero\n";
            } else {
                std::cout << "Result: " << a / b << '\n';
            }
            break;

        default:
            std::cout << "Unsupported operator\n";
    }

    return 0;
}

Example run

Enter an expression (e.g. 10 + 5): 10 + 5
Result: 15

Try these inputs:

  • 12 - 4

  • 6 * 7

  • 15 / 3

  • 8 / 0

The program checks for a zero divisor before performing floating-point division. For a production calculator, also validate input failures and decide how to handle unsupported operators and non-finite values.

3.12 Common mistakes

Mistake

Correct understanding

Confusing = with ==

Assignment is different from equality comparison.

Expecting 5 / 2 to produce 2.5

Integer division produces 2.

Confusing && with &

Logical AND differs from bitwise AND.

Ignoring precedence

Multiplication binds more tightly than addition.

Dividing by zero

Check the divisor before performing division.

Overcomplicating prefix/postfix expressions

Use simple statements to make evaluation easier to understand.

3.13 Hands-on exercises

Exercise 1 — Arithmetic operations

Accept two integers and display their sum, difference, product, quotient and remainder. Handle a zero divisor safely.

Exercise 2 — Comparison

Accept two numbers and print whether the first is greater than, less than or equal to the second.

Exercise 3 — Logical conditions

Accept a student's marks and attendance percentage. Use logical operators to check whether marks are at least 40 and attendance is at least 75%.

Exercise 4 — Prefix and postfix

Predict the output of this program before running it.

int x = 4;
std::cout << x++ << '\n';
std::cout << ++x << '\n';

Exercise 5 — Precedence

Calculate the results of 8 + 4 * 2 and (8 + 4) * 2. Explain why they differ.

Exercise 6 — Calculator extension

Extend the calculator to support the remainder operator for integer inputs and reject invalid input.

Show — Exercise 4 solutionShow — Exercise 5 solution

3.14 Module summary

In this module, you learned about:

  • Arithmetic and assignment operators.

  • Relational and logical operators.

  • Prefix and postfix increment/decrement.

  • Bitwise operations.

  • The conditional operator.

  • Precedence and associativity.

  • Expressions, type behaviour and division.

  • A practical calculator application.

Completion criteria: Students should be able to select suitable operators, predict expression results, explain operator precedence and write a basic calculator with appropriate input checks.

3.15 Knowledge-check quiz

What is the value of result after this code executes: int result = 8 + 4 * 2;?

1 of 5

A. 16

B. 24

C. 12

D. 20