Conditional Statements

Scott Waldron Updated by Scott Waldron

Dino defines several conditional statements that choose options based on the truth of an expression. In most places in Dino, an assignment expression (a = 5) is allowed. However, because the following types of errors are so common:

if a = 5 { // error, assignment not allowed in expression
}
else if a == 10 or b = 10 { //error, assignment not allowed in expression
}

Notice in the previous example, we are assigning the value 5 to the variable a (which then returns 5), and in the else if, assigning 10 to the variable b. Because this is nearly always a logic error and not what the user intended, Dino disallows an assignment expression as any part of the truth expression for a conditional statement. It must instead be written as follows:

if a == 5 { // ok
}
else if a == 10 or b == 10 { // ok
}

What is True?

Dino considers more than just true to be “true”, so naturally it also considers more than just false to be “false”. Conditional statements like if will instead evaluate the “truthiness” of an expression. The following values are “false” with respect to conditional expressions: false, null, 0 (of any numeric type), empty string, DBNull (a special .NET null type for database work), minimum date, empty GUID, empty time span, and empty sequence (list or enumerable). All other values are “true”.

if / else if / else

The if statement is defined in plain language as “if expression is true, do this, else if other expression is true, do this, else if all those are false, do this.”

// Dino if statement
var x = 10;
// while x is 'truthy'
while x {
if x % 2 == 0 {
print('The number is even.');
}
else if x == 5 {
print('The number is right in the middle.');
}
else {
print('The number is odd.');
}
x--; //decrement x by 1
}

Some languages allow a naked statement after the if or else expression. Dino does not allow this; you must put all evaluated statements in a block. One consequence of this is that the parentheses around the if or else expression are not required.

if a > 10
doSomething(); // error, naked statements not allowed
if a > 10 {
doSomething(); // ok, statements but be in block
}
if (a > 10) { // ok, parentheses are fine but not required
doSomething();
}

Ternary Operator (Expression)

The ternary operator is a conditional which has an expression on the LHS, an expression in the middle, and one on the RHS. Unlike the traditional if statement, the ternary is an expression: If LHS evaluates to true, then the middle expression is evaluated and returned, if LHS is false, then RHS is evaluated and returned.

The three expressions in a ternary are separated with the ? and : characters, e.g. lhs ? middle : rhs.

// Dino ternary
var a = true ? 3 : 4; // a will be 3 because LHS is truthy
var b = null ? 3 : 4; // b will be 4 because LHS is falsy

The ternary operator is usually referred to as “the” ternary operator, because in most languages it is the only operator that is not binary or unary. To see all the binary and unary operators, refer to the Operators section of this document.

The ternary expression is short-circuit evaluated: if the LHS expression results in true, then the RHS does not need to be evaluated.

How did we do?

Anonymous Functions

The Context Object

Contact