In this video, will learn about c++ expressions and statements.
Let's start with expressions.
Expressions are the most basic building blocks in programming.
Expressions are defined as follows in the c++ standard.
A sequence of operators and operands that specifies a computation.
We'll see some examples shortly. But for now you can think of an expression
as a sequence of operation operators that computer value.
That's a pretty simplistic view, but it will
serve us well until we learn more advanced c++ features.
Let's see some example c++ expressions.
Let's start with the literal 34.
That's an expression whose value is simply 34.
Variables, such as favorite number in this example, are also expressions
since their value is the value of the variable.
We can also have mathematical expressions, such as 1.5 + 2.8,
which will add those two numbers.
And 2 * 5, which will multiply 2 and 5.
There are many, many more examples of expressions
since they're many operators in c++,
and we can combine them to make any expression we wish.
If we wanted to test to see if A was greater than B,
where A and B are variables, we can use the greater than sign between the A and the B.
This evaluates to a Boolean, a true false value.
Notice the last example, this is an assignment expression.
We are assigning B to A.
This is also an expression because the value of this expression is what we just assign.
We'll talk more about this in a few videos when we talked about assignment statements in detail.
Now let's see what makes a statement different from an expression.
A statement is a complete line of code that performs some action.
Statements are usually terminated with a semicolon.
When you first learn c++,
it's a very common error to forget to terminate statements with semicolons.
Statements usually contain expressions.
As you would expect, c++ has many types of statements.
It has expression statements, the null statement, compound statements and more.
We'll discuss specific types of statements in the next several sections of the course.
Let's see some example c++ statements.
The first statement is the declaration statement,
which we've already seen in this course when we declared variables.
In this example, we declare x2b of type integer.
The second is a simple assignment statement,
where we assign the literal expression 12 to favorite number.
The third example is an expression statement.
Note that is simply an expression with a semicolon at the end.
It's a pretty useless statement though since we add 2 numbers, but don't do anything with the sum.
The fourth example is x equals 2 * 5 is an assignment statement.
First, we multiply 2 and 5 and store the product in x.
Notice how expression are used to make up the statement.
The next example is an if statement.
If statements give you lots of power to control the flow of your program.
We'll discuss if statements in detail.
But you can see that we're comparing A and B.
And if A is greater than B, then we execute the statement that displays to the console.
So here you can see an example of a statement
that contains another statement within it and is also made up of expressions.
The final statement is simply a semicolon.
This is the null statement and perform has no actions.
You don't often used in all statement, but it can come in handy sometimes.