2.12. Compound Expressions¶
So far we have looked at the elements of a programming language—variables, expressions, and statements—in isolation, without talking about how to combine them.
One of the most useful features of programming languages is their ability to take small building blocks and compose them. For example, we know how to multiply integers and we know how to output values; it turns out we can do both at the same time:
This program performs multiplication and prints the result simultaneously.
Actually, I shouldn’t say “at the same time”, since in reality the multiplication has to happen before the output, but the point is that any expression, involving numbers, characters, and variables, can be used inside an output statement. We’ve already seen one example:
This program performs a calculation involving variables and prints the result at the same time.
You can also put arbitrary expressions on the right-hand side of an assignment statement:
This program performs a calculation involving variables and simultaneously assigns the result as the variable initial value.
This ability may not seem so impressive now, but we will see other examples where composition makes it possible to express complex computations neatly and concisely.
Caution
There are limits on where you can use certain expressions; most notably, the left-hand side of an assignment statement has to be a variable name, not an expression.
That’s because the left side indicates the storage location where the
result will go. Expressions do not represent storage locations, only
values. So the following is illegal: minute + 1 = hour;
.
- Change line 5 to pets = dogs + cats;
- Assignment statements operate such that the evaluated expression on the right is assigned to the variable on the left.
- Change line 5 to int pets = dogs + cats;
- pets has already been declared as an int.
- Change line 5 to pets == dogs + cats;
- The == operator checks if the left side EQUALS the right side. It is not the correct operator here.
- Change line 5 to int pets == dogs + cats;
- pets has already been declared as an int. Also, the == operator is not the proper choice here.
- No change, the code runs fine as is.
- Assignment statements assign the value on the right to the variable on the left.
Q-4: What must be changed in order for this code block to work?
1int main () {
2 int dogs = 3;
3 int cats = 6;
4 int pets;
5 dogs + cats = pets;
6 cout << "I have " << pets << " pets!";
7 return 0;
8}
Q-5: The left-hand side of an assignment statement has to be a name, not an expression.
Q-6: In programming, another word for combine is .
Finish the code below so that the velocity is calculated and returned on the same line.
Hint: the current velocity results from 1) the initial velocity and 2) the acceleration over a window of time.
Finish the code below so that the volume of a cylinder with radius r
and height h
is calculated and returned on the same line.
More to Explore
From cppreference.com
C++ expressions and function declarations