A non-empty sequence of literals, variables, operators, and function calls that calculates a value
The process of executing an expression is called evaluation, and the resulting value produced is called the result of the expression (also sometimes called the return value)
# different expressions2 // 2 is a literal that evaluates to value 2"Hello world!" // "Hello world!" is a literal that evaluates to text "Hello world!"x // x is a variable that evaluates to the value held by variable x2 + 3 // operator+ uses operands 2 and 3 to evaluate to value 5five() // evaluates to the return value of function five()
expressions evaluating to different things
literals evaluate to their own values
variables evaluate to the value of the variable
operators (such as operator+) use their operands to evaluate to some other value
function calls evaluate to whatever value the function returns
expressions are in statements
type identifier { expression };
// 2 + 3 is an expression that has no semicolon -- the semicolon is at the end of the statement containing the expressionint x{ 2 + 3 };
Expression statements
a statement that consists of an expression followed by a semicolon
Thus, we can take any expression (such as x = 5), and turn it into an expression statement (x = 5;) that will compile.
When an expression is used in an expression statement, any result generated by the expression is discarded (because it is not used). For example, when the expression x = 5 evaluates, the return value of operator= is discarded. And that’s fine, because we just wanted to assign 5 to x anyway.
Subexpressions, full expressions, and compound expressions
2 // 2 is a literal that evaluates to value 22 + 3 // 2 + 3 uses operator + to evaluate to value 5x = 4 + 5 // 4 + 5 evaluates to value 9, which is then assigned to variable x
subexpression is an expression used as an operand.
the subexpressions of x = 4 + 5 are x and 4 + 5. The subexpressions of 4 + 5 are 4 and 5.
full expression is an expression that is not a subexpression.
All three expressions above (2, 2 + 3, and x = 4 + 5) are full expressions.
In casual language, a compound expression is an expression that contains two or more uses of operators.
x = 4 + 5 is a compound expression because it contains two uses of operators (operator= and operator+). 2 and 2 + 3 are not compound expressions.