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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

38 CHAPTER 2. <strong>C++</strong> BASICS<br />

<strong>for</strong> (...; ...; ...) {<br />

...<br />

if (dx == 0.0) continue;<br />

x+= dx;<br />

...<br />

if (r < eps) break;<br />

...<br />

}<br />

In the example above we assumed that the remainder of the iteration is not needed when<br />

dx == 0.0. In some iterative computations it might be clear in the middle of an iteration (here<br />

when r < eps) that work is already done.<br />

Understanding the program behavior becomes more difficult the more breaks and continues<br />

are used. One should always aim <strong>for</strong> moving as much loop control as possible into the loop<br />

head. However, avoiding breaks and continues by excessive if-then-else branches is even less<br />

comprehensible.<br />

Sometimes, one might prefer per<strong>for</strong>ming some surplus operations inside a loop (if it has no<br />

perceivable impact on the overall per<strong>for</strong>mance) and keep the program simpler. Simpler programs<br />

on the other hand have a better chance to get optimized by the compiler. There is certainly<br />

no golden rule but as practical approach one should implement software first <strong>for</strong> maximal<br />

clarity and simplicity (but using efficient algorithms as early as possible). Once the software is<br />

working correctly one can try variations to investigate the impact of implementation details on<br />

per<strong>for</strong>mance.<br />

2.5.6 Switch Statement<br />

A switch is like a special kind of if. It provides a concise notation when different computations<br />

<strong>for</strong> different cases of a given integral value are per<strong>for</strong>med:<br />

switch(op code) {<br />

case 0: z= x + y; break;<br />

case 1: z= x − y; cout ≪ ”compute diff\n”; break;<br />

case 2:<br />

case 3: z= x ∗ y; break;<br />

default: z= x / y;<br />

}<br />

When people see the switch statement <strong>for</strong> the first time, they are usually surprised that one<br />

needs to say at end of each case that the statement is terminated. Otherwise the statements of<br />

the next case are executed as well. This can be used to per<strong>for</strong>m the same operation <strong>for</strong> different<br />

cases, e.g. <strong>for</strong> 2 and 3 in the example above.<br />

The continuation allows us also to implement short loops without the termination test after<br />

each iteration. Say we have vectors with dimension ≤ 5. Then we could implement a vector<br />

addition without a loop:<br />

assert(size(v)

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

Saved successfully!

Ooh no, something went wrong!