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.5. CONTROL STATEMENTS 37<br />

Here it was simpler to take out term 0 and start with term 1. We also used less-equal to assure<br />

that the term x 10 /10! is considered.<br />

The <strong>for</strong>-loop in C ++ is very flexible. The initialization part can be any expression, a variable<br />

declaration or empty. It is possible to introduce multiple new variables of the same type. This<br />

can be used to avoid repeating the same operation in the condition, e.g.:<br />

<strong>for</strong> (int i= xyz.begin(), end= xyz.end(); i < end; i++) ...<br />

Variables declared in the initialization are only visible within the loop and hide variables of the<br />

same names from outside the loop.<br />

The condition can be any expression that can be converted to a bool. An empty condition is<br />

always true and the loop is repeated infinitely unless from inside the body as we will discuss<br />

in the next section. We said that loop indices are typically incremented in the head’s third<br />

part. In principle, one can modify it within the loop body but programs are much clearer if it<br />

is done in the loop head. On the other hand, there is no limitation that only one variable is<br />

increased by 1. One can modify as many variables as wanted using the comma operator and by<br />

any modification desired such as:<br />

<strong>for</strong> (int i= 0, j= 0, p= 1; ...; i++, j+= 4, p∗= 2) ...<br />

This is of course more complex than having just one loop index but still more readable than<br />

declaring/modifying indices be<strong>for</strong>e the loop or inside the loop body.<br />

In fact, the <strong>for</strong>-loop in C and C ++ is just another notation of a while-loop. Any <strong>for</strong>-loop:<br />

<strong>for</strong> (init; cond; incr) {<br />

st1; st2; ... stn;<br />

}<br />

can be written with a while-loop:<br />

{<br />

}<br />

init;<br />

while (cond) {<br />

st1; st2; ... stn;<br />

incr;<br />

}<br />

Conversely, any while-loop can evidently be written as <strong>for</strong>-loop. We do not know if there is<br />

a design guideline from a software engineering guru when to use while or <strong>for</strong> but <strong>for</strong> is more<br />

concise if there is a local initialization or some incremental operation.<br />

2.5.5 Loop Control<br />

There are two statements to deviate from the regular loop evaluation:<br />

• break and<br />

• continue.<br />

A break terminates the loop entirely and continue ends only the current iteration and continues<br />

the loop with the next iteration, <strong>for</strong> instance:

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

Saved successfully!

Ooh no, something went wrong!