Control Flow

Conditionals and loops in Tenet.


If Statements

if (condition) {
    // then branch
} else {
    // else branch (optional)
}

Chained Conditions

if (score >= 90) {
    print "A";
} else if (score >= 80) {
    print "B";
} else if (score >= 70) {
    print "C";
} else {
    print "F";
}

While Loops

var i = 0;
while (i < 5) {
    print i;
    i = i + 1;
}

For Loops

C-style for loops:

for (var i = 0; i < 5; i = i + 1) {
    print i;
}

All three clauses are optional:

var i = 0;
for (; i < 5;) {
    print i;
    i = i + 1;
}

Logical Operators

AND (Short-Circuit)

true and true;   // true
true and false;  // false
false and true;  // false (right side not evaluated)

OR (Short-Circuit)

true or false;   // true (right side not evaluated)
false or true;   // true
false or false;  // false

NOT

!true;   // false
!false;  // true
!nil;    // true (nil is falsy)

Short-Circuit Evaluation

false and expensive();  // expensive() never called
true or expensive();    // expensive() never called

This is useful for guard conditions:

if (user != nil and user.isAdmin) {
    // Safe: user.isAdmin only checked if user exists
}

Comparison Operators

OperatorDescription
==Equal
!=Not equal
<Less than
<=Less than or equal
>Greater than
>=Greater than or equal

Next Steps