- [Narrator] In C++ a statement is a unit of code.
Here I have a working copy of statement.cpp
from chapter two of the exercise files.
And for example, down here on line 10
is a statement which calls the library function printf.
A statement is analogous to a line of code.
It may do many things
but it's treated as a unit of execution.
A statement is always terminated with a semi-colon.
An expression is anything that returns a value
whether or not the value is used.
An expression may be part of a statement
or it may be an entire statement.
Here I have an expression actually,
it's also a statement which declares
the variable X of type integer.
And here I have an expression
it's also a statement which assigns the value 42
to the variable X.
And here we have a statement that includes
an expression that simply returns the value of X.
And here we have another statement then includes
an expression that both assigns the value 73
to the variable X and then returns that value
for use in the printf function.
If I build and run this,
you'll notice that we get this result
X is 42 and X is 73.
This printf function prints these values out to the console.
And it prints these values using this expression
as a template and it includes this replacement
which is then replaced from the value of X.
And we'll get into the syntax
of the printf statement a little later in this course
but for now you see we're printing the value of X
which is assigned as 42 up on line eight
and then we're printing it again
after first assigning the value of 73 to it.
So you can see how these expressions
and statements work together.
All of this is a statement
and this part of it is an expression
because it's something that returns a value.
And that value is used
as an argument to the function called printf.
An expression can include operators and parentheses.
Like here, I could say 42 times 12 and plus 14.
And then if I build and run this
you notice we get 518 for that value of X
or I can put parentheses around the addition,
just like in mathematics, multiplication happens
before addition unless you put parentheses around it
and I can save and run this
and you'll notice that X is now 1092.
An expression can include operators and parentheses
and all of these different things to return a value.
We'll discuss details like operator precedence
and things like that later in this course.
An expression is anything that returns a value
an expressions may be part of a statement
or even maybe an entire statement.