Conditional statement (if)
By default, program flow in BI# is sequential. BI# runs the statements
one by one in their order. To change the program flow, use an
if
statement:
int Sign(int x) { if (x > 0) { return 1; } else { return -1; } }
In the function above the program flow depends on the value of the
function parameter
x
. If
x
is greater than 0, the function
returns 1. Otherwise, it returns -1.
An
if
statement consists at least of a
Boolean condition and a block of statements that must be run if the condition
evaluates to true.
The
else
clause is optional, but if it
exists, BI# runs the statements in the block that belong to the else clause, if
the condition evaluates to false.
This example shows how you can cascade several conditions by using
the
else if
clause:
void CountDigits(int x) { if (x > 0 and x < 10) { WriteLine("One digit."); } else if (x >= 10 and x < 100) { WriteLine("Two digits."); } else { WriteLine("More than two digits."); } }