Concepts / Statements vs. Expressions

Statements vs. Expressions

An expression is a combination of values, variables, and operators, or a single value or variable by itself.

  • Programming

A Value Is Already an Expression

When learners first hear the word expression, they often imagine a long calculation containing several operators. An expression can be much simpler than that. A bare value such as 42 is an expression, and a variable name such as score is also an expression. Both can be evaluated to produce a result.

Definition

An expression is a combination of values, variables, and operators, or a single value or variable by itself.

42valueresultvalue producedscorevariable nameevaluates to itselfuses stored value
How can a lone value or variable qualify as an expression even when it contains no operator?

Parts of a Compound Expression

A compound expression combines values, variables, and operators. The values or variables being acted on are the operands. The operator tells the program what operation to perform on those operands. In score + 10, score is one operand, 10 is the other operand, and + is the operator.

scoreoperand: variablenew resultproduced by evaluation+operator10operand: valueoperandcombines withparticipates inparticipates in
How are values, variables, and operators connected within an expression, and what role does each part play?
Worked example

Labeling total multiplied by 2

Identify the pieces of the expression total * 2.

  1. 1
    Variable operand

    total is one operand because it is a variable used by the expression.

  2. 2
    Operator

    * is the multiplication operator. It specifies the operation performed with the operands.

  3. 3
    Value operand

    2 is the other operand because it is a literal value used by the expression.

  4. 4
    Evaluation

    The program substitutes the current value of total, performs the multiplication, and returns the result.

Result

The expression contains a variable operand, a multiplication operator, and a value operand.

Substitution Before Evaluation

A variable inside an expression stands for its current stored value. Before the operation is completed, the program substitutes that current value for the variable. This is why the same expression can produce different results when the stored values of its variables differ.

Worked example

Evaluating a plus expression

Suppose a is 8 and b is 3. Evaluate the expression a + b.

  1. 1
    Start with the expression

    The expression is a + b. The variable names are the operands, and + is the operator.

  2. 2
    Substitute current values

    Replace a with 8 and b with 3, producing 8 + 3.

  3. 3
    Apply the operator

    Apply the plus operator to the substituted values.

Result

The expression evaluates to 11.

a + ba = 8, b = 38 + 3variable values substituted11evaluated resultsubstitute current valuesapply +
What changes as variable values are substituted and the operator is applied step by step?

Results and Variable Changes

Evaluating an expression produces a result, but that evaluation does not change the original variable by itself. For example, evaluating score + 10 creates a new result based on the current value of score. The original variable remains unchanged unless the result is explicitly assigned back to it.

SituationWhat happens to the expressionWhat happens to the original variable
Evaluate an expressionA result is producedIt does not change automatically
Assign the result backThe result is used as the assigned valueThe variable changes because the result is explicitly assigned

The source-grounded distinction between producing a result and changing a variable.

score + 10produces a resultnew resultdoes not change score byitselfexplicit assignmentuses the resultscorestored value changesevaluateassign explicitlychange stored value
What is the difference between producing a result and explicitly changing the stored variable?

Common Recognition Mistakes

  • Common mistake

    Assuming an expression must contain an operator.

    What it looks like

    Treating only score + 10 as an expression while overlooking 42 and score.

    Why it fails

    A single value or a single variable name is also an expression.

    Do this instead

    Check first whether the item is a value or variable. It can qualify as an expression even without an operator.

  • Common mistake

    Treating a variable name as if it were the result of an expression.

    What it looks like

    Reading a + b without considering the current values stored in a and b.

    Why it fails

    The program substitutes each variable with its current stored value before applying the operator.

    Do this instead

    Write the substituted values first, then apply the operator.

  • Common mistake

    Assuming that evaluating an expression changes its variables.

    What it looks like

    Expecting score + 10 to change score automatically.

    Why it fails

    An expression produces a result but does not change the original variable unless the result is explicitly assigned back.

    Do this instead

    Separate the result of evaluation from any later explicit assignment.

  • Common mistake

    Ignoring the roles of the expression's parts.

    What it looks like

    Describing total * 2 without identifying total, *, and 2.

    Why it fails

    The variable and value are operands, while the operator specifies the operation.

    Do this instead

    Label each operand and the operator before tracing the evaluation.

Best practice

When an expression seems confusing, trace it in three steps: identify its operands, substitute the current stored values of any variables, and apply the operator to produce the result.

Practice the Evaluation Trace

EASY

Suppose points is 12 and bonus is 5. Trace the expression points + bonus. Name the operands and operator, show the expression after substituting the current values, and state the result. Then explain whether evaluating the expression alone changes points.

Hints
  • The operands are the variable names before substitution.
  • Replace points with 12 and bonus with 5 before applying +.
  • Use the distinction between producing a result and explicitly assigning it back.

What do you think happens?

If a is 8 and b is 3, what result does a + b produce after substitution?

  • 5

  • 11

  • a + b

  • The original variables change automatically

Reveal answer
Answer:

11

The program substitutes a with 8 and b with 3, producing 8 + 3, and then applies the plus operator. Evaluating the expression produces 11 without automatically changing the original variables.

Key Takeaways

  • An expression can be a combination of values, variables, and operators, or a single value or variable by itself.
  • Values and variables used by an operator are operands; the operator specifies what operation to perform.
  • When a variable appears in an expression, its current stored value is substituted before evaluation.
  • Evaluating an expression produces a result, but it does not change the original variable unless the result is explicitly assigned back.
  • A reliable evaluation trace identifies the parts, substitutes current values, and then applies the operator.

Related concepts

More concepts starting with S