Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Introduction to expressions
Master combining values, variables, and operators to create expressions that produce results.
Prerequisites
1.10 — Introduction to expressions
What is an expression?
An expression is a combination of literals, variables, operators, and function calls that can be evaluated to produce a result. Every expression has a value and a type.
#include <iostream>
int main() {
// These are all expressions:
5 // literal expression, value: 5
3 + 4 // arithmetic expression, value: 7
2 * 6 - 1 // complex arithmetic expression, value: 11
return 0;
}
Simple expressions
Literal expressions
The simplest expressions are just literals:
5 // integer expression with value 5
3.14 // floating-point expression with value 3.14
"Hello" // string expression with value "Hello"
Variable expressions
Variables by themselves are expressions:
#include <iostream>
int main() {
int x = 10;
x // variable expression with value 10
std::cout << x << std::endl; // x is evaluated to 10
return 0;
}
Arithmetic expressions
Arithmetic expressions use operators to combine values:
#include <iostream>
int main() {
// Simple arithmetic expressions
std::cout << 3 + 5 << std::endl; // 8
std::cout << 10 - 4 << std::endl; // 6
std::cout << 7 * 2 << std::endl; // 14
std::cout << 15 / 3 << std::endl; // 5
return 0;
}
Complex expressions
You can combine multiple operators and values:
#include <iostream>
int main() {
std::cout << 2 + 3 * 4 << std::endl; // 14 (not 20!)
std::cout << (2 + 3) * 4 << std::endl; // 20 (parentheses change order)
std::cout << 10 - 3 + 2 << std::endl; // 9
std::cout << 8 / 2 * 3 << std::endl; // 12
return 0;
}
Expressions with variables
Variables can be part of expressions:
#include <iostream>
int main() {
int a = 5;
int b = 3;
std::cout << a + b << std::endl; // 8
std::cout << a * 2 << std::endl; // 10
std::cout << a + b - 1 << std::endl; // 7
std::cout << a * b + 10 << std::endl; // 25
return 0;
}
Expressions in assignments
The right side of an assignment is always an expression:
#include <iostream>
int main() {
int x = 5; // 5 is an expression
int y = x + 3; // x + 3 is an expression (evaluates to 8)
int z = x * y - 10; // x * y - 10 is an expression (evaluates to 30)
std::cout << "x: " << x << std::endl; // x: 5
std::cout << "y: " << y << std::endl; // y: 8
std::cout << "z: " << z << std::endl; // z: 30
return 0;
}
Order of operations (operator precedence)
C++ follows mathematical rules for order of operations:
- Parentheses first:
()
- Multiplication and Division next:
*
/
(left to right) - Addition and Subtraction last:
+
-
(left to right)
#include <iostream>
int main() {
std::cout << 2 + 3 * 4 << std::endl; // 14 (3*4=12, then 2+12=14)
std::cout << (2 + 3) * 4 << std::endl; // 20 (2+3=5, then 5*4=20)
std::cout << 10 - 6 / 2 << std::endl; // 7 (6/2=3, then 10-3=7)
std::cout << (10 - 6) / 2 << std::endl; // 2 (10-6=4, then 4/2=2)
return 0;
}
Expressions produce values
Every expression "evaluates to" or "produces" a value:
#include <iostream>
int main() {
// Each expression produces a value that gets assigned
int result1 = 5 + 3; // 5 + 3 evaluates to 8
int result2 = 10 * 2; // 10 * 2 evaluates to 20
int result3 = result1 + result2; // 8 + 20 evaluates to 28
std::cout << result1 << std::endl; // 8
std::cout << result2 << std::endl; // 20
std::cout << result3 << std::endl; // 28
return 0;
}
Using parentheses for clarity
Even when not required, parentheses can make expressions clearer:
#include <iostream>
int main() {
int a = 10, b = 5, c = 2;
// These are equivalent, but second is clearer:
int result1 = a + b * c; // 20
int result2 = a + (b * c); // 20 (clearer intention)
// Complex expression made clearer with parentheses:
int complex = (a + b) * (c + 1); // (10+5) * (2+1) = 15 * 3 = 45
std::cout << result1 << std::endl; // 20
std::cout << result2 << std::endl; // 20
std::cout << complex << std::endl; // 45
return 0;
}
Common mistakes with expressions
Forgetting order of operations
// What you might expect: (2 + 3) * 4 = 20
// What actually happens: 2 + (3 * 4) = 14
int wrong = 2 + 3 * 4;
// Use parentheses to be explicit:
int right = (2 + 3) * 4; // 20
Integer division
#include <iostream>
int main() {
// Integer division truncates (cuts off) decimal part
std::cout << 7 / 2 << std::endl; // 3 (not 3.5!)
std::cout << 9 / 4 << std::endl; // 2 (not 2.25!)
return 0;
}
Expressions everywhere
Expressions appear in many places in C++:
#include <iostream>
int main() {
int x = 10;
int y = 5;
// In assignments
int sum = x + y;
// In output statements
std::cout << x * 2 << std::endl;
// In calculations
int average = (x + y) / 2;
// Multiple expressions in one statement
std::cout << "Sum: " << x + y << ", Product: " << x * y << std::endl;
return 0;
}
Summary
- Expressions combine literals, variables, and operators to produce values
- Every expression has a value and a type
- Order of operations matters: parentheses, then *//, then +/-
- Expressions can be simple (like
5
) or complex (like(a + b) * (c - d)
) - Use parentheses to make intentions clear and control order
- Expressions appear everywhere in C++: assignments, output, calculations
Understanding expressions is crucial because most of C++ programming involves creating and evaluating expressions to solve problems.
Introduction to expressions - Quiz
Test your understanding of the lesson.
Practice Exercises
Building and Evaluating Expressions
Practice creating expressions that combine values, variables, and operators.
Lesson Discussion
Share your thoughts and questions