If-Else

Fig 1:


var rng = random::forSeed(1);
var r = random::nextNonNegativeBoundedInt(rng, 100);

var s: string;
if (r < 33) { # Curly braces are mandatory.
    s = "small";
} else if (r < 66) {
    s = "medium";
} else {
    s = "large";
}

print(s); # Prints "small", "medium", or "large".

Output:

large

Possible use of an uninitialized variable is a compile-time error:

Fig 2:


var rng = random::forSeed(1);
var r = random::nextNonNegativeBoundedInt(rng, 100);

var s: string;
if (r < 33) {
    s = "red";
} else if (r < 66) {
    s = "green";
}

print(s); # `s` is uninitialized if r >= 66.

Compilation Errors:

if_else_EX2_example.claro:11: Variable <s> may not have been initialized!
print(s); # `s` is uninitialized if r >= 66.
      ^
Warning! The following declared symbols are unused! [s]
2 Errors

Note: Claro's error messaging is a work in progress - the above error message will be improved.