03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.4. EXPRESSIONS AND STATEMENTS 31<br />

A= Bˆ2;<br />

A is B squared. So far so good. That the original meaning of ˆ is a bitwise XOR does not<br />

worry us because we do not plan implementing bitwise operations on matrices.<br />

Now we add C:<br />

A= Bˆ2 + C;<br />

Looks nice. But does not work (or does something weird). — Why?<br />

Because + has a higher priority than ˆ. Thus, the compiler understands our expression as:<br />

A= B ˆ (2 + C);<br />

Oops. That looks wrong. 9 The operator gives a concise and intuitive interface but its priority<br />

would cause a lot of confusion. Thus, it is advisable to refrain from this overloading.<br />

2.4 Expressions and Statements<br />

C and C ++ distinguish between expressions and statements. Very casually spoken, one could<br />

just say that every expression becomes a statement if an semicolon is appended. However, we<br />

would like to discuss this topic a bit more.<br />

Let us build this recursively from bottom up. Any variable name (x, y, z, . . . ), constant or<br />

literal is an expression. One or more expressions with an operator is an expression, e.g. x + y or<br />

x ∗ y + z. In several languages, e.g. Pascal, the assignment is a statement. In C and C ++ it is an<br />

expression, e.g. x= y + z. As a consequence it can be used in another assignment: x2= x= y + z.<br />

Assignments are evaluated from right to left. Input and output operations as<br />

std::cout ≪ ”x is ” ≪ x ≪ ”\n”;<br />

are also expressions.<br />

A function call with expressions as arguments is an expression, e.g. abs(x), abs(x ∗ y + z). There<strong>for</strong>,<br />

function calls can be nested: pow(abs(x), y). In languages where a function call is a statement<br />

this would not be possible. As the assignment is an expression, it can be used as argument of a<br />

function: abs(x= y). Or I/O operations as those above. Needless to say that this is quite bad programming<br />

style. An expression surrounded by parenthesis is an expression as well, e.g. x + y.<br />

This allows us to change the order of evaluation, e.g. x ∗ (y + z) computes the addition first<br />

although the multiplication has the higher priority.<br />

A very special operator in C ++ is the ‘comma operator’ that provides a sequential evaluation.<br />

The meaning is simply evaluating first the sub-expression left of the comma and then that right<br />

of it. The value of the whole expression is that of the right sub-expression. The sub-expressions<br />

can contain the comma operator as well so that arbitrarily long sequences can be defined. With<br />

the help of the comma operator, one can evaluate multiple expressions in program locations<br />

where only one expression is allowed. If used as function argument it the comma expression<br />

needs surrounding parentheses; otherwise the comma is interpreted as separation of function<br />

arguments. The comma operator can be overloaded with a user-defined semantics. This can<br />

9 The precise interpretation is A.operator=(operatorˆ(B, operator+(2, C)));

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!