statements ‘if’, ‘for’, ‘while’, and ‘repeat-until’

If, for, while and repeat-until

The syntax of the ‘if’ and ‘while’ statements are shown in this example:

if n%2 == 0 { s = “even” } else { // the else part is optional s = “odd” } var i = 0; while i < 5 { Out println: i; ++i }

 

Note that ( ) as not necessary for the ‘if’ and ‘while’ expressions but the { } are mandatory.

Be aware that indentation must be respected when using these statements. See the Cyan manual for rules regarding the if statement. In particular, the } that closes a while statement should be either in the same line as keyword while or in the same column as it. There should be no semicolon after any } symbol.

There is a repeat-until statement that executes its statements until the until expression evaluates to true

repeat sum = sum + n; ++n; until n >= 4;

 

For statement for can be used to iterate over any object that implements method iterator -> Iterator<T>. Arrays and intervals implement it:

for elem in [ 2, 3, 5, 7 ] { “$elem is prime” println } var s = 0; for n in 1..100 { s = s + n } assert s == 5050;