[Phy 405/905 Home Page] [ Lecturer ]

2. Statements and Expressions

Now we're getting into the meat and potatoes of C/C++. Statements are the building blocks of source code; statements in turn contain expressions. We've already seen a simple example of a statement (variable declaration/definition) and of an expression (primary expression, in particular, literals). In this section we'll survey the majority of the diverse kinds of statements and expressions. Together with a future lectures on pointers and structures, the material up to this point covers the basics of the C-subset of C++ (and after those lectures, we move on to object-oriented programming ... finally!)


What's a statement/what's an expression?

statement
what we usually mean by a "line" of code. Actually, possibly several lines, because one type of statement, a function definition statement, obviously includes a group of statements itself.
expression
From ARM [[section]]5, "an expression is a sequence of operators and operands that specifies a computation. An expression may result in a value and may cause side effects." [my emphasis - the "side effects" are a curious convenience in C/C++]
I'll introduce the different kinds of statements and expressions in a somewhat circular fashion, but you can check out the textbook for a more straightforward presentation.

Simplest (and most basic) kind of statement, i.e., the simplest line of code is the

expression statement
a line of code of the form:
expression ;
where out of all the different kinds of expressions, the simplest is the primary expression, which is:
primary expression
a literal, a name, or a "global-scoped" name like ::my_global_var.
So some simple expression statements:
	// some statements
; // null statement! do nothing! have as many of these as you like!!
4; // my compiler doesn't complain:
// ... an expression with *no* "side-effect"
some_var;
(some_var); // exactly identical to the previous line
Before we get too far, here's a very important kind of expression:
assignment expression
an expression involving one of the assignment operators (e.g. '=' or '*='), of the form "lhs = rhs", where 'lhs' is a modifiable lvalue (object) of the same type as the expression 'rhs', and ''=" signifies any assignment operator
Because rhs in this definition is an expression, here's an interesting (assignment) expression statement:
	a = b = c = d = 0.0; // note: the = operator groups from right to left
The meaning of this is (d=0.0), then (c=d), then (b=c), then (a=b). If you have ever programmed in assembly language, you can see how a statement like this could be easily translated into very tight machine code.

Avery rapid overview of some of the important operators and the type of expressions they yield:

These operators definitely have side-effects, which can be strung together:
a = i++ + j++ + k++; // well-defined
b = (i++ + (j++ + i++)); // no particular result guaranteed  
The second line is ambiguous because the parentheses do not change the order of evaluation of each subexpression (when, precisely, is each of the values of i in the second line determined?). The compiler will likely warn you if it looks like you've introduced this kind of ambiguity.

"Side-effect" operators such as these are very useful (and very often used).

Some other types of expressions:

With the relational, equality, and logical operators defined, we can consider further types of statements.

compound statement
one or more statements enclose in {}. Defines its own local scope in the enclosed block.
selection statement
one of:

if (expression) statement
if (
expression) statement else statement
switch(
expression) statement

where statement can be a single line (but must not be a declaration), or any group of lines in a compound statement.

if (expression) statement

Expression must be arithmetic type, and the statement following will be executed iff expression is non-zero. Here's an example:


if (a > 0)
--q;
if (b > 0)
{
i= j+1;
f(i);
}

if (expression) statement else statement

Exactly what you expect. You can chain these together:

if (a > 0)
;
else if (a==0) { // block statement!
f(a);
q(a);
}
else if (b > a)
f(b);
else
foo_func(a,b);
The final "else" is not mandatory, since if it were absent, that would simply make the preceeding "else if" be interpreted as "else (if (expression) statement)".

I'll defer discussion of the switch statement for the moment; let's first check out two of the three kinds of iteration statement, while and do...while

while (expression) statement

The expression must be of arithmetic type. The statement gets executed as long as the expression is non-zero, where the test is carried out before the statement gets executed:

while (1)
; // infinite loop!
while (a > 0 && --a)
;
while (notdone())
{
// do stuff, hopefully someday affecting the return value of notdone()
}

do statement while (expression);

Like the previous while loop, except that the test occurs after each time the statement gets excecuted, guaranteeing that statement will be carried out at least once. Note the final ';'
do
transform(a);
while (a > 0);

do
{
double q = sin(4.0);

// do other stuff
} while ( !done()) // note use of !

[ Phy 405/905 Home Page] [Lecturer ]