if-(else)
Use if-(else) statements to select a statement or block of code to execute based on a condition.
General Form
If-statements have the following general forms:
if (condition) consequence
if (condition) consequence else alternative
if (condition0) consequence0
else if (condition1) consequence1
..
else if (conditionN) consequenceN
else alternative
where condition is a boolean expression and consequence and alternative are statements. The consequence is executed if condition is true. When several conditions exist with else if, the conditions are evaluated in order. When a condition is true and the corresponding consequence has been executed, processing stops. If no conditions are true, the alternative will be executed instead.
Type Conversions and Assignments
The condition can also contain type conversions and assignments like this:
id = expr
type id = expr
The consequence statement will only be executed if expr is not null, or has type bool and value true. The identifier id is only visible inside the consequence block, if it was declared inside the condition using the second form.
Casting
The condition can also contain type casting, using the keyword as, like this:
id as type
The consequence statement will only be executed if the casting of the identifier id into type was successful. Inside the consequence block, id is of type.
Example
{ AShape2D shape = APolyline2D(); // fillColor() is final in AShape2D and can be called directly. pln(shape.fillColor()); if (shape as APolyline2D) { // count() extend APolyline2D and type casting is necessary. pln(#shape.count()); } }
Type Checking
The condition can also contain type checking (without casting), using the keyword in, like this:
id in type
The consequence statement will only be executed if id is of type. The initial typing of id will be maintained, even inside the consequence block.
Example
{ AShape2D shape = APolyline2D(); if (shape in APolyline2D) { pln("shape is a polyline!"); //pln(#shape.count()); // Would give compile error } }
See also: Type Tests and Conversions.
switch-case
Use switch statements to select a statement or block of code to execute based on comparison with a set of constant values. switch statements are a structured alternative to chains of if-else statements. They can help to detect programming mistakes such as repeated cases, and improve performance.
General Form
switch(expr) { caseClause1 caseClause2 .. caseClausen }
where caseClause can have the forms
case literal: stmt case literal1, literal2, .., literaln: stmt
default: stmt
and literal is a literal of a supported type. No case may be repeated. The supported types are:
- all integer types except nat and int64
- all enum types
- integer ranges
- str
- symbol
The default case is chosen when the expression does not match any of the other cases. Failure to match an alternative will throw an exception if the default case was omitted.
Examples
{ int x = 1; switch(x) { case 1: pln("one"); case 2: pln("two"); default: pln("other"); } }
{ switch(x) { case 1, 3, 5: { pln("odd"); } case 2, 4, 6: { pln("even"); } default: pln("other"); } }
{ switch(x) { case 0..9: pln("one digit"); case 10..99: pln("two digits"); } }
{ str s = getStr(); switch(s) { case "SE": lang = "swedish"; case "FR": lang = "french"; default: ; } }
{ symbol sym; switch(sym) { case #"cm.core": pkg = "core"; case #"cm.win": pkg = "win"; default: pkg = "fail"; } }
Comments
0 comments
Please sign in to leave a comment.