Beginner 10 min read

What Is an Expression?

By the end of this lesson, you'll be able to:

  • Define "expression" and distinguish it from a statement
  • Identify expressions inside a line of Apex code
  • Explain that every expression evaluates to a single value

Prerequisites: Module 3: "Programming Basics: Variables & Data"

Expressions vs. statements

Integer total = 4 + 3;

4 + 3 is an expression — it evaluates to a single value, 7. The whole line, Integer total = 4 + 3;, is a statement — a complete instruction that does something (here, declares a variable and stores the expression's result in it).

Every expression, however complicated, boils down to one value once Apex finishes evaluating it.

Expressions can nest inside each other

Integer result = (4 + 3) * 2;

4 + 3 is an expression. (4 + 3) * 2 is a bigger expression, built out of the first one. This is exactly how the rest of this module works: small operators combine into bigger, more useful expressions.

Common mistakes

  • Thinking a variable name alone isn\'t an expression. It is — total by itself evaluates to whatever value it currently holds, which is why System.debug(total); works at all.

Exercise

Write down three different expressions that would each evaluate to the number 10 (for example, 5 + 5). Try to make them structurally different from each other.

Show hint

Addition, multiplication, and subtraction can all reach 10 in different ways: 5+5, 2*5, 20-10.

What Is an Expression? Quiz

1. What does every expression eventually do?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

An expression is any piece of code that evaluates down to a single value — a number, a piece of text, true or false. Every operator you'll meet in this module exists to combine expressions into bigger ones.