In this section, we’ll introduce some ﬂow control structures that will prove handy later. Most of them are inspired by C/C++.
The if construct is the same has C/C++’s one. The if keyword is followed by a condition between parentheses and an expression, and optionally the else keyword and another expression. If the condition evaluates to true, the ﬁrst expression is evaluated. Otherwise, the second expression is evaluated if present.
The if construct is an expression: it has a value.
The while construct is, again, the same as in C/C++. The while keyword is followed by a condition between parentheses and an expression. If the condition evaluation is false, the execution jumps after the while block; otherwise, the expression is evaluated and control jumps before the while block.
The for keyword supports diﬀerent constructs, as in languages such as Java, C#, or even the forthcoming C++ revision.
The ﬁrst construct is hardly more than syntactic sugar for a while loop.
The second construct allows to iterate over members of a collection, such as a list. The for keyword, followed by var, an identiﬁer, a colon (or in), an expression and a scope, executes the scope for every element in the collection resulting of the evaluation of the expression, with the variable named with the identiﬁer referring to the list members.
The syntax of the switch construct is similar to C/C++’s one, except it works on any kind of object, not only integral ones. Comparison is done by semantic equality (operator ==). Execution will jump out of the switch-block after a case has been executed (no need to break). Also, contrary to C++, the whole construct has a value: that of the matching case.
A do scope is a shorthand to perform several actions on an object.
The same result can be obtained with a short do scope, that redirect method calls to their target, as in the listing below. This is similar to the Pascal “with” construct. The value of the do-block is the target itself.