In Verse, control flow works differently than in most traditional programming languages. It is heavily tied to the concept of Failure, where expressions don't just return true or false—they either succeed (and produce a value) or fail (and halt execution in that context). [1]
This tutorial covers the essential control flow structures in Verse: conditional expressions, loops, and failure contexts. [2, 3]
1. Conditionals: if-else
In Verse,
if is an expression, meaning it can return a value. The condition inside an if statement is a failure context. If any expression inside the condition fails, the if block is skipped, and the else block runs. [4, 5] Basic if-else Syntax
Note the
? after IsGameOver. In Verse, to check a logic variable in a condition, you must use the ? operator to test if it succeeds (is true). [6, 7] Using if as an Expression
Because
if is an expression, you can assign its direct result straight to a variable: [8] 2. Multi-branching: case
When you need to check a single variable against multiple potential values, use the
case expression. It matches the value against different patterns sequentially. [9] 3. Loops and Iteration
Verse provides multiple ways to repeat actions, from infinite loops to filtering data arrays. [10]
A. The loop Expression (Infinite Loops)
The
loop keyword repeats a block of code indefinitely. To exit a loop, you must use the break keyword. [11, 12] B. The for Expression (Iteration & Filtering)
The
for expression is highly versatile. It can iterate over ranges or arrays, and it natively supports built-in filter conditions right inside the loop definition. [13] Basic Range Iteration
Iteration with a Filter
If a condition inside the
for generator fails, that specific iteration is skipped, and the loop moves seamlessly to the next item.4. Failure Contexts and decisions
The most unique aspect of Verse control flow is the
decides effect. Functions or expressions marked with decides are fallible—meaning they are allowed to fail. [14] You can safely run fallible expressions only inside a failure context, such as an
if condition or a for loop filter. [15] Complete Control Flow Blueprint
The following example combines
loop, for with filters, and fallible array indexing into a cohesive game script: