- Installing
- Hello World
- Spaces
- Identation
- Basics
- Comments
- Output
- ++ method
- Assignments and types
- Operator Precedence
- String interpolation
- Input
- Simple arithmetic
- Multiline strings
- Assert
- Arrays and literal arrays
- Literal intervals
- If, for, while and repeat-until
- Message sends
- Conversion between basic type values
- Anonymous functions
- Evaluating an anonymous function
- Functions with method foreach
- Prototypes
- Packages
- Gradual typing
- Generic prototypes
- Important metaobjects
- The CyanInterpreter prototype
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;