- Declarative Statements
- Expression Statements
- Assignment Statements
- Selective Statements
- Iteration Statements
- Jump Statements
- Exception Handling Statements
Declarative Statements
// Variable declaration without initial value. double noValueYet; // Variable declaration with initial value. double initValue = 1.22674; // Constant declaration statement. const double pi = 3.141592654;
Expression Statements
// Assignment. area = 3.14 * (radius * radius); // Method invocation. snapperName.invalidate; snapperName.invalidate(); // New object creation and assignment (Only for non-primitive data types). buf = new StrBuf(); buf = StrBuf(); init buf(); init buf(64);
There is also a conditional init, which allows you to replace this:
if (!buf) init buf();
With this:
init? buf();
Assignment Statements
Standard assignment in CM is done with the = operator.
extend public void operator=(type name) { } extend public void operator+=(type name) { } extend public void operator<<(type name) { }
...
Assignment with numeric types can be done with all other operators and can of course be overridden on subclasses classes as well.
Selective Statements
See also Control Flow article.
if .. else
if (condition) { consequence(s) } else if (condition) { consequence(s) } else { consequence(s) } if (condition) one-line consequence else if (condition) one-line consequence else one-line consequence
switch .. case
switch (expression) { case (value1) : { consequence(s) } case (value2) : { consequence(s) } default : { consequence(s) } } switch (expression) { case (value1) : one-line consequence case (value2) : one-line consequence default : one-line consequence }
A switch-statement consists of three parts. A switch part containing an expression and block, [1, N] case parts followed by an optional default part. If the expression in the switch part results in a value that is not covered by the cases and there is no default block defined, a missing default exception will be thrown.
Some other languages give the possibility to break in case blocks. In CM the break will be implicitly done after a case block has been executed.
Iteration Statements
See also Control Flow article.
for
for (controlVariable in values) { consequence(s) }
while
while (condition) {
consequence(s)
}
Another possibility is to include initialization and/or increment in the while statement.
while (initialization; condition; increment) {
consequence(s)
}
do .. while
do { consequence(s) } while condition
Jump Statements
See also Control Flow article.
break
for (x in 1..3) { pln(x); break; }
Output:
1
continue
for (x in 1..3) { if (c == 2) continue; pln(x); }
Output:
1
3
goto
goto JumpHere; ... JumpHere: ...
Exception Handling Statements
See also Control Flow article.
try .. catch .. finally
try { consequence(s) } catch (Exception e) { exception handling } finally { cleanup }
do .. finally
do { consequence(s) } finally { cleanup }
throw
if (isSomethingWrong) throw Exception("Insert exception message here");
Comments
0 comments
Please sign in to leave a comment.