Expressions
Consider the following series of statements:
1 2 3 4 |
int x{ 2 }; // initialize variable x with value 2 int y{ 2 + 3 }; // initialize variable y with value 5 int z{ (2 * 3) + 4 }; // initialize variable z with value 10 int w{ y }; // initialize variable w with value 5 (the current value of variable y) |
Each of these statements defines a new variable and initializes it with a value. Note that the initializers shown above make use of a variety of different constructs: literals, variables, and operators. Somehow, C++ is converting each of these literals, variables, and operators into a single value that can be used as the initialization value for the variable.
What do all of these have in common? They make use of an expression.
An expression is a combination of literals, variables, operators, and explicit function calls (not shown above) that produce a single output value. When an expression is executed, each of the terms in the expression is evaluated until a single value remains (this process is called evaluation). That single value is the result of the expression.
Here are some examples of different kinds of expressions, with comments indicating how they evaluate:
1 2 3 4 5 6 |
2 // 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 of x 2 + 3 // 2 + 3 uses operator + to evaluate to value 5 x = 2 + 3 // 2 + 3 evaluates to value 5, which is then assigned to variable x std::cout << x // x evaluates to the value of x, which is then printed to the console |
As you can see, literals evaluate to their own values. Variables evaluate to the value of the variable. We haven’t covered function calls yet, but in the context of an expression, function calls evaluate whatever value the function returns. And operators let us combine multiple values together to produce a new value.
Note that expressions do not end in a semicolon, and cannot be compiled by themselves. For example, if you were to try compiling the expression x = 5, your compiler would complain (probably about a missing semicolon). Rather, expressions are always evaluated as part of statements.
For example, take this statement:
1 |
int x{ 2 + 3 }; // 2 + 3 is an expression that has no semicolon -- the semicolon is at the end of the statement containing the expression |
If you were to break this statement down into its syntax, it would look like this:
1 |
type identifier { expression }; |
Type could be any valid type (we chose int). Identifier could be any valid name (we chose x). And expression could be any valid expression (we chose 2 + 3, which uses 2 literals and an operator).
Key insight
Wherever you can use a single value in C++, you can use an expression instead, and the compiler will resolve the expression down to a single value.
Expression statements
Certain expressions (like x = 5) are useful by themselves. However, we mentioned above that expressions must be part of a statement, so how can we use these expressions by themselves?
Fortunately, we can convert any expression into an equivalent statement (called an expression statement). An expression statement is a statement that consists of an expression followed by a semicolon. When the statement is executed, the expression will be evaluated (and the result of the expression will be discarded).
Thus, we can take any expression (such as x = 5), and turn it into an expression statement (such as x = 5;) that will compile.
Note that we can make expression statements that compile but are meaningless/useless (e.g. 2 * 3;). This expression evaluates to 6, and then the value 6 is discarded.
Rule
Values calculated in an expression are discarded at the end of the expression.
Quiz time
Question #2
Indicate whether each of the following lines are statements that do not contain expressions, statements that contain expressions, or are expression statements.
a)
1 |
int x; |
b)
1 |
int x = 5; |
c)
1 |
x = 5; |
d) Extra credit:
1 |
std::cout << x; // Hint: operator<< is a binary operator. |
Question #3
Determine what values the following program outputs. Do not compile this program. Just work through it line by line in your head.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { std::cout << 2 + 3 << '\n'; int x{ 6 }; int y{ x - 2 }; std::cout << y << '\n'; int z{ 0 }; z = x; std::cout << z - x << '\n'; return 0; } |
![]() |
![]() |
![]() |
Sorry, did not look carefully, my question is duplicate to one just below.
S1: Wherever you can use a single value in C++, you can use an expression instead, and the compiler will resolve the expression down to a single value.
Q: Why in most places it is mentioned that compiler resolve's the expressions ?? Isn't it done at runtime where compiler is not involved.
Eg: y = x + 10; ( suppose x is a variable input by user, how can compiler evaluate the expression at compile time ?? )
Correct me if i understood the statement S1 wrongly.
"Wherever you can use a single value in C++, you can use an expression instead, and the compiler will resolve the expression down to a single value."
Isn't this false in the case of function calls, which are resolved at runtime and not at compiletime?
If this is an expression statement, what does this evaluate to?(since all expression statements evaluate to something)
I tried doing this:
which gave me 2 errors that basically said it was impossible to convert an object of whatever class cout is part of into a char, and that it was not possible to do during initialization.
Isn't there anyway to check what kind of value
evaluates to? Or is it not possible because cout is a special object?
`std::cout <<` returns `std::cout`. You can't store `std::cout` in a `char`.
Isn't there anyway to "print" cout? No?
No
Thanks
Hi Alex just some clarification regarding your explanation here does the same concept apply to question three
Is there a meaning to separate "expression statement" and the other statements? I mean only this thing isn't called /*a whole statement is an expression*/ but /*an expression statement*/ in question 2. Could I get it to continue the learning?
What does
mean as an expression?; what does it evaluate to? I understand how literals evaluate to themselves (ie, 5 evaluates to 5), but wouldn't the above code just take the expression of a literal (5) and assign it to the variable x?
If that's the case, wouldn't
just be a statement that contains an expression (5), rather than an "expression statement"?
Apparently it evaluates to whatever the right operand evaluates to, not sure why:
the expression evaluates to x (and returns x)
"Values calculated in an expression are discarded at the end of the expression."
Are those values stored temporally in a memory or in a register?
That's not standardized.
Hello Nescar,
I have to say this tutorial is the best of all the programming tutorials I've ever seen. Thank you!
For this one:
#include <iostream>
int main()
{
int x{ 6 };
int y{ x - 2 };
std::cout << y << '\n';
return 0;
}
6 and x-2 are both expressions, the values after evaluation are assigned to x and y respectively. Why the values calculated in an expression are discarded? Does it mean like these values are no longer stored in the memory? Would you please elaborate on it?
Thanks in advance.
Thank you
"Wherever you can use a single value in C++, you can use an expression instead, and the compiler will resolve the expression down to a single value."
What if we had a function call returns a reference type (code in the below), so in that case we can't use a single value for the whole function call but we can use an expression, so isn't this in contrast to what you stated above? on the other hand, if we have a regular function call, then what you stated was correct, but how about this example?
I said wherever you can use a single value, you can use an expression instead. I did not say all expressions can be replaced by single values.
You said that "the compiler will resolve the expression down to a single value".
It may be true in cases like
(There will be just 4 in compiled code, without any addition).
But in most cases like
the compiler cannot resolve the expression. There'll be the same function calls and operation of addition in compiled code. Resolving to a single value is performing in runtime in this case, not in compile time.
hello,in question #2 what you explain is rational,but it also performs and action which is printing the value of x?so how can it just be a expression statement ?(since the result is shown and not discarded).
clear me if I've got something wrong thx.
Can these be called "Expression statements"?
- Please explain the reason.
They look like malformed statements to me.
Are these intended to be definitions (in which case, x needs a type specifier before it)?
Sir, please forget my previous question. I am asking you again-
1. In a program, does this x need a type specifier before using it?
2. From Question 3: at line 12, why can't I write- z{x}; ?
3. Which kinds of initializations and assignments can be used in Expression statements?
1. if you haven't declared it first, yes.
2. you can do it, the author just wanted it to be tricky.
3. it's the contrary: expressions that can be used in initializations and assignments