What is an Expression?
You'll learn: What an expression is and how it produces a single value through computation.
The Core Idea
An expression is a combination of values, variables, and operators that a computer evaluates to produce a single result. Think of it like a math problem you solve: 2 + 3 is an expression that evaluates to 5.
Every expression asks the computer to calculate something and give back an answer. That answer is always a single value, even if the expression looks complicated.
Expressions in Action
When you write code, you're constantly building expressions:
x + 10— combines a variable and a value with an operator5 * 3— multiplies two integers, evaluates to15temperature > 100— compares values, evaluates to a boolean (trueorfalse)firstName— even a single variable is an expression! It evaluates to whatever value is stored in that variable
Notice each example produces one result when the computer evaluates it.
Expressions vs Statements
An expression always produces a value. You can think of expressions as questions with answers. Later you'll learn about statements (like variable declarations), which are more like commands or actions that don't necessarily produce a value.
For now, remember: if it calculates something and gives you back a result, it's an expression.
Expressions Build Up
Simple expressions can combine into larger ones:
(x + 5) * 2 uses the expression x + 5 as part of a bigger expression. The computer evaluates from the inside out, getting one final answer.
Key Takeaway: An expression is any combination of values, variables, and operators that the computer evaluates to produce a single result.