Skip to main content

Conditional Statements

If / Else

The basic conditional structure in Soul:

Truthiness

Soul follows these rules for truthiness:

Falsy Values

  • false
  • null
  • 0
  • "" (empty string)

Truthy Values

  • true
  • Any non-zero number
  • Any non-empty string
  • Lists and Maps (even empty)
  • Functions

Ternary Operator

For simple conditions, use the ternary operator:

Switch Statements

For multiple conditions on the same value:
Soul requires explicit break statements to prevent fall-through between cases.

Loops

While Loop

Executes while a condition is true:

For Loop

Traditional C-style for loop:

For-In Loop

The most idiomatic way to iterate in Soul:

Iterating Lists

Iterating Maps

Range Iteration

Loop Control

Break

Exit a loop early:

Continue

Skip to the next iteration:

Error Handling

Try / Catch

Handle errors gracefully:

Finally Block

Execute code regardless of success or failure:

Pattern Examples

Early Return Pattern

Guard Clauses

Loop with Index and Value