Using conditionals
Conditionals are control structures that allow us to separate the main execution flow into one or more secondary flows of execution from a Boolean evaluation.
Bosque provides if
, elif
, and else
reserved words for writing a conditional block. Additionally, due to Bosque being a curly-bracket language, we have to use curly brackets instead of a reserved word for closing blocks.
The following figure shows different ways to define a conditional:
The only restriction in the construction of conditional blocks is that if we use an elif
block, it is mandatory to have one else
block.
Let's see an example:
if ( x > 10 ) { return "error, greater than 10"; elif ( x < 0 ) { return "error, the number must not be negative"; } else { return "it's ok, thanks"; }
In this...