IndexNextUpPreviousUrbi SDK 3.0.0

Chapter 5
Flow Control Constructs

In this section, we’ll introduce some flow control structures that will prove handy later. Most of them are inspired by C/C++.

 5.1 if
 5.2 while
 5.3 for
 5.4 switch
 5.5 do

5.1 if

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 first expression is evaluated. Otherwise, the second expression is evaluated if present.

 
if (true) 
  echo("ok"); 
[00000000] *** ok 
if (false) 
  echo("ko"
else 
  echo("ok"); 
[00000000] *** ok  

The if construct is an expression: it has a value.

 
echo({ if (false) "a" else "b" }); 
[00000000] *** b  

5.2 while

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.

 
var x = 2; 
[00000000] 2 
while (x < 40) 

  x += 10; 
  echo(x); 
}; 
[00000000] *** 12 
[00000000] *** 22 
[00000000] *** 32 
[00000000] *** 42  

5.3 for

The for keyword supports different constructs, as in languages such as Java, C#, or even the forthcoming C++ revision.

The first construct is hardly more than syntactic sugar for a while loop.

 
for (var x = 2; x < 40; x += 10) 
  echo(x); 
[00000000] *** 2 
[00000000] *** 12 
[00000000] *** 22 
[00000000] *** 32  

The second construct allows to iterate over members of a collection, such as a list. The for keyword, followed by var, an identifier, 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 identifier referring to the list members.

 
for (var e : [1, 2, 3]) { echo(e) }; 
[00000000] *** 1 
[00000000] *** 2 
[00000000] *** 3  

5.4 switch

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.

 
switch ("bar"

  case "foo":  0; 
  case "bar":  1; 
  case "baz":  2; 
  case "qux":  3; 
}; 
[00000000] 1  

5.5 do

A do scope is a shorthand to perform several actions on an object.

 
var o1 = Object.clone(); 
[00000000] Object_0x423a0708 
var o1.one = 1; 
[00000000] 1 
var o1.two = 2; 
[00000000] 2 
echo(o1.uid); 
[00000000] *** 0x423a0708  

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.

 
var o2 = Object.clone(); 
[00000000] Object_0x42339e08 
// All the message in this scope are destined to o. 
do (o2) 

  var one = 1; // var is a shortcut for the setSlot 
  var two = 2; // message, so it applies on obj too. 
  echo(uid); 
}; 
[00000000] *** 0x42339e08 
[00000000] Object_0x42339e08