Skip to study material
← Question index

QUESTION 67 · Expressions and conversions

Distinguish an expression from an expression statement.

2 marks

Answer

An expression performs an operation, such as calculating a sum or assigning a value. A semicolon after an expression forms an expression statement.

  • a + b is an expression. a + b; is a statement that discards the calculated sum.
  • total = 5; is an expression statement that stores 5. int total = 5; is a declaration with initialisation.

QUESTION 68 · Expressions and conversions

Define an expression and explain how its type affects the result.

5 marks

Answer in brief

An expression combines values, variables, operators or function calls to perform a calculation or another operation.

Parts of an expression

  1. Operands: The values or objects used, such as a, b and 10.
  2. Operators: The operations performed, such as + or *.
  3. Parentheses: Show the intended grouping, such as (a + b) * 2.

Types and examples

  1. Arithmetic: a + b * 2 calculates a numeric result.
  2. Relational: a > b produces 1 or 0.
  3. Logical: a > 0 && b > 0 tests two conditions.
  4. Assignment: total = 5 stores a value.
  5. Conditional: a > b ? a : b selects a value.

Expression and expression statement

a + b is an expression. Adding a semicolon gives the expression statement a + b;. That statement calculates the sum and discards it. total = a + b; is useful because it stores the result. int total = 5; is a declaration with initialisation, rather than an expression statement.

Evaluation example

For a = 4 and b = 3, a + b * 2 gives 10 because multiplication groups before addition. (a + b) * 2 gives 14. Operand types also matter: 7 / 2 gives 3, whereas 7.0 / 2 gives 3.5. Avoid expressions that change the same variable without the ordering required by C.

Reference table

↔ Scroll horizontally on a small screen
Expression examples
ExpressionWhat it does
a + b * 2Computes an arithmetic value.
score >= 40Produces an int truth value, 0 or 1.
total = 5Stores a value and has the assigned value as its result.
count++Modifies count and supplies its previous value.
printf("Hello")Calls a function; the call also has a return value.

Key points

  • Grouping, types and sequencing all matter.
  • An expression can have a value and also change program state.