Topic 13 of 91 2. C++ Fundamentals Interactive Tutorial

Expressions in C++: Operands, Operators, and Evaluation

15 mins read Last updated: 26 Sep 2026 18,900 views

Direct Answer & Core Concept

When writing a C++ program, you often need to perform calculations, compare values, assign data to variables, or check conditions. These operations are performed using something called expressions.

For example, when you write 10 + 5, C++ evaluates the expression and produces the result 15.

But how does C++ understand this calculation? What are operands and operators? And how does the compiler decide which operation should happen first?

To answer these questions, you need to understand expressions in C++.

In this tutorial, you will learn what expressions are, how operands and operators work together, the different types of expressions, operator precedence, associativity, and how C++ evaluates expressions with practical examples.

Detailed Explanation & Mechanics

What Is an Expression in C++?

An expression in C++ is a combination of values, variables, operators, and sometimes function calls that produces a result or performs an operation.

Expressions are used in almost every C++ program.

For example:

main.cpp
Run Online
10 + 5
a * b
x - y
(price + tax) / 2

Each of these is an expression because it represents an operation that C++ can evaluate.

Example: A Simple Expression in C++

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 5;

    int result = a + b;

    cout << "Result: " << result;

    return 0;
}

Output:

In this program, a + b is an expression. C++ adds the values of the two variables and produces the result 15.

What Are Operands and Operators in C++?

To understand expressions, you first need to know two important terms: operands and operators.

1. What Is an Operand?

An operand is a value, variable, or other expression on which an operator performs an operation.

For example:

Here:

  • 10 is the first operand.

  • 5 is the second operand.

  • + is the operator.

Operands can be numbers, variables, constants, or the results of other expressions.

2. What Is an Operator?

An operator is a symbol or keyword that tells C++ what operation to perform.

For example:

In this expression, the + operator adds the values of a and b.

Other common operators include:

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Remaindera % b
==Equal toa == b
>Greater thana > b
&&Logical ANDa && b

Types of Expressions in C++

C++ supports different types of expressions depending on the operators and values being used.

The commonly studied categories include:

  1. Arithmetic expressions

  2. Relational expressions

  3. Logical expressions

  4. Assignment expressions

  5. Conditional expressions

  6. Bitwise expressions

  7. Increment and decrement expressions

  8. Comma expressions

Let's understand each type with simple examples.

1. Arithmetic Expressions in C++

Arithmetic expressions are used to perform mathematical calculations.

C++ provides arithmetic operators such as addition, subtraction, multiplication, division, and modulus.

Common Arithmetic Operators

OperatorOperationExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5
/Division10 / 5
%Modulus10 % 3

Example: Arithmetic Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 20;
    int b = 10;

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

    return 0;
}

Output:

main.cpp
Run Online
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Remainder: 2

Explanation

  • a + b adds two numbers.

  • a - b subtracts the second number from the first.

  • a * b multiplies two numbers.

  • a / b divides the first number by the second.

  • a % 3 returns the remainder after division.

Important: When both operands are integers, division produces an integer result. For example, 10 / 3 produces 3, not 3.33.

To get a decimal result, use a floating-point operand:

2. Relational Expressions in C++

Relational expressions are used to compare two values.

They produce a Boolean result: true or false.

These expressions are commonly used in decision-making statements such as if, while, and for.

Common Relational Operators

OperatorMeaningExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example: Relational Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 20;
    int b = 10;

    cout << (a > b) << endl;
    cout << (a < b) << endl;
    cout << (a == b) << endl;
    cout << (a != b) << endl;

    return 0;
}

Output:

By default, C++ displays true as 1 and false as 0 when using cout.

You can display readable Boolean values using boolalpha:

main.cpp
Run Online
cout << boolalpha << (a > b);

Output:

3. Logical Expressions in C++

Logical expressions are used to combine or reverse conditions.

They are particularly useful when a program needs to check multiple conditions before performing an operation.

C++ provides three main logical operators.

Common Logical Operators

OperatorNameMeaning
&&Logical ANDTrue when both conditions are true
``Logical ORTrue when at least one condition is true
!Logical NOTReverses a Boolean value

Example: Logical Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int age = 25;
    int salary = 30000;

    cout << (age >= 18 && salary >= 20000) << endl;

    cout << (age < 18 || salary >= 20000) << endl;

    cout << !(age >= 18) << endl;

    return 0;
}

Output:

Explanation

  1. The first expression checks whether both conditions are true.

  2. The second expression checks whether at least one condition is true.

  3. The third expression reverses the result of the condition.

Short-Circuit Evaluation

C++ uses short-circuit evaluation with logical AND (&&) and logical OR (||).

This means the second condition may not be evaluated if the first condition already determines the final result.

Example:

main.cpp
Run Online
int a = 10;
int b = 0;

if (b != 0 && a / b > 2) {
    cout << "Valid";
}

Here, the second condition is not evaluated because b != 0 is false.

This prevents division by zero in this example.

4. Assignment Expressions in C++

Assignment expressions are used to assign values to variables.

The assignment operator is =.

C++ also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment.

Common Assignment Operators

OperatorMeaningExample
=Assign valuea = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 2
/=Divide and assigna /= 2
%=Modulus and assigna %= 3

Example: Assignment Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int number = 10;

    number += 5;

    cout << "After Addition: " << number << endl;

    number *= 2;

    cout << "After Multiplication: " << number << endl;

    return 0;
}

Output:

main.cpp
Run Online
After Addition: 15
After Multiplication: 30

The += operator adds a value to the existing variable, while *= multiplies the current value and assigns the result back.

5. Conditional Expressions in C++

The conditional operator ?: is also known as the ternary operator because it uses three operands.

It is used to choose one of two values based on a condition.

Syntax

main.cpp
Run Online
condition ? expression1 : expression2;

If the condition is true, expression1 is selected. Otherwise, expression2 is selected.

Example: Conditional Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int age = 20;

    string result = (age >= 18) ? "Adult" : "Minor";

    cout << result;

    return 0;
}

Output:

Explanation

In this example:

  • The condition checks whether age is greater than or equal to 18.

  • If true, the expression returns "Adult".

  • If false, it returns "Minor".

The conditional operator is useful for simple decisions where you need to choose between two expressions.

6. Bitwise Expressions in C++

Bitwise expressions operate on the individual bits of integer values.

They are commonly used in low-level programming, embedded systems, networking, and situations where bit-level operations are required.

Common Bitwise Operators

OperatorMeaning
&Bitwise AND
``Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example: Bitwise Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 3;

    cout << "AND: " << (a & b) << endl;
    cout << "OR: " << (a | b) << endl;
    cout << "XOR: " << (a ^ b) << endl;
    cout << "Left Shift: " << (a << 1) << endl;
    cout << "Right Shift: " << (a >> 1) << endl;

    return 0;
}

Output:

main.cpp
Run Online
AND: 1
OR: 7
XOR: 6
Left Shift: 10
Right Shift: 2

Explanation Using Binary Values

The number 5 in binary is 0101, and the number 3 is 0011 when shown using four bits.

  • AND produces 0001, which is 1.

  • OR produces 0111, which is 7.

  • XOR produces 0110, which is 6.

  • Left shifting 5 by one position produces 10.

  • Right shifting positive 5 by one position produces 2.

7. Increment and Decrement Expressions in C++

Increment and decrement operators are used to increase or decrease a variable's value by one.

C++ provides two forms of each operator.

Increment Operators

  • ++a – Prefix increment.

  • a++ – Postfix increment.

Decrement Operators

  • --a – Prefix decrement.

  • a-- – Postfix decrement.

Example: Increment and Decrement

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 10;

    cout << "Original Value: " << a << endl;

    cout << "Prefix Increment: " << ++a << endl;

    cout << "Postfix Increment: " << a++ << endl;

    cout << "Final Value: " << a << endl;

    return 0;
}

Output:

main.cpp
Run Online
Original Value: 10
Prefix Increment: 11
Postfix Increment: 11
Final Value: 12

Difference Between Prefix and Postfix

Prefix increment (++a):

The variable is increased first, and then its updated value is used.

Postfix increment (a++):

The original value is used first, and then the variable is increased.

The same distinction applies to prefix and postfix decrement.

8. Comma Expressions in C++

The comma operator , allows multiple expressions to be evaluated from left to right.

The result of a comma expression is the result of its rightmost expression.

Example: Comma Expression

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a, b, result;

    result = (a = 10, b = 20, a + b);

    cout << "Result: " << result;

    return 0;
}

Output:

Explanation

In this example:

  1. a = 10 is evaluated first.

  2. b = 20 is evaluated next.

  3. a + b is evaluated last.

  4. The final result is assigned to result.

The comma operator is different from the commas used to separate function arguments or variable declarations.

What Is Expression Evaluation in C++?

Expression evaluation is the process by which C++ determines the value or effect of an expression.

For example:

C++ does not simply evaluate every operation from left to right. It follows language rules such as operator precedence, associativity, and sequencing.

In this example, multiplication has higher precedence than addition.

Therefore:

The final result is 20.

Understanding these rules helps you predict how expressions behave.

Operator Precedence in C++

Operator precedence determines which operators are grouped first when an expression contains multiple operators.

For example:

Multiplication has higher precedence than addition, so 5 * 2 is grouped first.

Common Operator Precedence Table

The following table shows common C++ operators from higher precedence to lower precedence.

PriorityOperatorsDescription
1()[].->Primary and member access
2++--!~+-*&Unary operators
3*/%Multiplication, division, modulus
4+-Addition, subtraction
5<<>>Bitwise shifts
6<<=>>=Relational operators
7==!=Equality operators
8&Bitwise AND
9^Bitwise XOR
10``Bitwise OR
11&&Logical AND
12``Logical OR
13?:Conditional operator
14=+=-=*=/=%=Assignment operators
15,Comma operator

This is a simplified reference for commonly used operators. Some operators share precedence levels, and C++ has additional operators and details.

What Is Operator Associativity in C++?

Operator associativity determines how operators of the same precedence are grouped when they appear together.

For example:

The subtraction operator has left-to-right associativity.

Therefore, the expression is grouped as:

Left-to-Right Associativity

Most arithmetic operators use left-to-right associativity.

Example:

main.cpp
Run Online
int result = 100 / 10 / 2;

Evaluation grouping:

Right-to-Left Associativity

Assignment operators use right-to-left associativity.

Example:

main.cpp
Run Online
int a, b, c;

a = b = c = 10;

This is grouped as:

All three variables receive the value 10.

Important: Associativity determines grouping, not necessarily the order in which every operand or function call is evaluated. C++ has separate sequencing rules. Avoid writing expressions that modify the same variable multiple times without a clear sequencing relationship.

Difference Between Operator Precedence and Associativity

These two concepts work together, but they have different purposes.

FeatureOperator PrecedenceOperator Associativity
MeaningDetermines which operators are grouped firstDetermines grouping when operators share precedence
PurposeEstablishes operator priorityResolves grouping within the same priority
Example10 + 5 * 220 - 5 - 3
Result2012

How to Control Expression Evaluation Using Parentheses

Parentheses are used to control how an expression is grouped.

They make complex expressions easier to understand and help avoid mistakes.

Example Without Parentheses

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int result = 10 + 5 * 2;

    cout << "Result: " << result;

    return 0;
}

Output:

Example With Parentheses

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int result = (10 + 5) * 2;

    cout << "Result: " << result;

    return 0;
}

Output:

Parentheses change the grouping of the expression, so the addition is performed before multiplication.

Complete C++ Program Using Different Expressions

Now let's combine several expression types in one practical program.

C++ Code

main.cpp
Run Online
#include <iostream>
using namespace std;

int main() {

    int a = 10;
    int b = 5;

    // Arithmetic expression
    int sum = a + b;

    // Relational expression
    bool isGreater = a > b;

    // Logical expression
    bool result = (a > 5 && b < 10);

    // Assignment expression
    a += 2;

    // Conditional expression
    string status = (a > b) ? "Greater" : "Smaller";

    cout << "Sum: " << sum << endl;
    cout << "Is Greater: " << boolalpha << isGreater << endl;
    cout << "Logical Result: " << result << endl;
    cout << "Updated A: " << a << endl;
    cout << "Status: " << status << endl;

    return 0;
}

Output

main.cpp
Run Online
Sum: 15
Is Greater: true
Logical Result: true
Updated A: 12
Status: Greater

Explanation

This program demonstrates five different expression types:

  • Arithmetic expression calculates the sum of two values.

  • Relational expression compares two variables.

  • Logical expression checks multiple conditions.

  • Assignment expression updates the value of a variable.

  • Conditional expression selects a result based on a condition.

These expression types are used regularly in real-world C++ applications.

Common Mistakes When Using Expressions in C++

Beginners sometimes make mistakes when writing expressions. Here are some common problems and how to avoid them.

1. Confusing Assignment and Equality Operators

Incorrect assumption:

main.cpp
Run Online
if (a = 10) {
    cout << "Equal";
}

The = operator assigns a value. It does not compare two values.

Correct example:

main.cpp
Run Online
if (a == 10) {
    cout << "Equal";
}

Use == when you want to check whether two values are equal.

2. Ignoring Operator Precedence

Consider:

The result is 20, not 30, because multiplication has higher precedence than addition.

Use parentheses when you want a different grouping.

3. Performing Integer Division

Example:

The result is 3 because both operands are integers.

Use a floating-point operand if you need a decimal result:

4. Dividing by Zero

Incorrect example:

main.cpp
Run Online
int a = 10;
int b = 0;

int result = a / b;

Integer division by zero has undefined behaviour.

Always check the divisor before performing division.

5. Writing Confusing Expressions

Avoid writing very long expressions that are difficult to understand.

Instead, break complex calculations into smaller steps using meaningful variable names.

6. Confusing Logical and Bitwise Operators

Remember:

  • && means logical AND.

  • & means bitwise AND.

  • || means logical OR.

  • | means bitwise OR.

These operators have different purposes and should not be used interchangeably.

Best Practices for Writing Expressions in C++

Follow these simple practices when working with C++ expressions:

  • Use meaningful variable names.

  • Use parentheses to make complex expressions easier to understand.

  • Understand operator precedence before combining multiple operators.

  • Avoid unnecessary complicated expressions.

  • Use == for comparison and = for assignment.

  • Check for division by zero.

  • Use floating-point values when decimal calculations are required.

  • Understand the difference between logical and bitwise operators.

  • Avoid modifying the same variable multiple times in an unsequenced expression.

  • Break complicated calculations into smaller statements.

Frequently Asked Questions About Expressions in C++

1. What is an expression in C++?

An expression is a valid combination of operands, operators, and sometimes function calls that produces a value or performs an operation.

2. What are operands in C++?

Operands are the values or variables on which operators perform operations. For example, in 10 + 5, the numbers 10 and 5 are operands.

3. What are operators in C++?

Operators are symbols or keywords that perform specific operations on operands. Examples include +, -, *, /, ==, and &&.

4. What are the different types of expressions in C++?

Common expression categories include arithmetic, relational, logical, assignment, conditional, bitwise, increment, decrement, and comma expressions.

5. What is expression evaluation in C++?

Expression evaluation is the process of determining the value or effect of an expression according to C++ language rules.

6. What is operator precedence in C++?

Operator precedence determines how operators are grouped in an expression. Operators with higher precedence are grouped before operators with lower precedence.

7. What is operator associativity in C++?

Operator associativity determines how operators with the same precedence are grouped. For example, subtraction uses left-to-right associativity.

8. Why are parentheses used in C++ expressions?

Parentheses control the grouping of an expression. They help specify the intended calculation order and improve code readability.

9. What is the difference between logical and relational expressions?

Relational expressions compare values and produce a Boolean result. Logical expressions combine or reverse Boolean conditions.

10. What is a conditional expression in C++?

A conditional expression uses the ternary operator ?: to select one of two expressions based on a condition.

11. Can an expression contain multiple operators?

Yes. An expression can contain multiple operators and operands. C++ uses precedence and associativity rules to determine how the expression is grouped.

12. What is the difference between an expression and a statement?

An expression produces a value or performs an operation. A statement is a complete instruction executed by the program. An expression can form part of a statement.

Runnable Code Example: Expressions in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>
#include <utility>

void processValue(const std::string& lval) {
    std::cout << "Lvalue bind: " << lval << "
";
}

void processValue(std::string&& rval) {
    std::cout << "Rvalue / Move bind: " << rval << "
";
}

int main() {
    std::string message = "Persistent lvalue";
    processValue(message); // Calls lvalue overload

    processValue("Temporary prvalue"); // Calls rvalue overload
    processValue(std::move(message));  // std::move converts to xvalue -> rvalue overload

    // Short-circuit evaluation demonstration
    int* ptr = nullptr;
    if (ptr != nullptr && *ptr == 10) {
        // Safe: *ptr is never evaluated because left side is false
    } else {
        std::cout << "Short-circuit evaluation prevented null pointer dereference.
";
    }
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the difference between an lvalue and an rvalue in C++?

A: An lvalue has an identifiable memory address that can be inspected with the address-of operator `&` and persists beyond a single expression. An rvalue is a temporary value that does not persist and can be moved from.
Q

Q: Why is short-circuit evaluation critical for pointer safety?

A: It allows testing pointer validity `ptr != nullptr && ptr->isValid()` in a single boolean expression without risking segmentation faults when `ptr` is null.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Rely on short-circuit evaluation for safe guard checks.
2. Use std::move to explicitly cast lvalues to xvalues when ownership transfer is intended.
3. Avoid expressions with side-effects on the same variable without sequence points (e.g. i = i++).