2. First Programs
Expressions, Primary and Compound, and Parentheses
Round brackets, also called parentheses, can be used to group expressions:
cout << (3 + (28-2)*5)*2;
Should we carry out this statement, we will get the following result:
STDOUT: 266
Before you get any ideas, we should warn you that it is not allowed in C++ to use square brackets to group expressions. It is also not allowed to use curly brackets, also called braces, to group expressions. For example;
cout << [3 +(28-2)*5]*2; // INCORRECT
Let's now explain the structure of expressions. The simplest kind of an expression is a literal expression. A literal expression is the one stating its value literally. No computation is required to figure out the value of a literal expression.[*]
More precisely, no computation is required during run-time (What is run-time? We will explain that later). The given description of literals is fine for now, although not the most correct one.
For example, number 5 is an expression by itself and it is a literal expression. Its value equals 5, obviously. Here is an example:
int main() { cout << "This number is a literal expression: " << 24 << endl; cout << "This string is a literal expression, too!" << endl; }
It results in:
STDOUT: This number is a literal expression: 24 This string is a literal expression, too!
As the example says, strings are literal expressions, because the value of a string is literally stated between the quotation marks.
Literals are one kind of primary expressions. An expression is either a primary
expression or a compound expression. Compound
expressions are created by combining other expressions, or
by modifying other expressions with operators. For example, the expression 5 + 2*6
is a compound expression. It is made of the literal expression 5
, the
operator +
and the compound expression 2*6
. In turn, the compound expression 2*6
is made
of the literal expression 2
, the multiplication operator and the literal expression 6
.