Skip to content

Conditionnal block

AxelHumeau edited this page Jan 14, 2024 · 2 revisions

How to write a conditionnal block ?

Condition in LobsterLang is written this way:

# basic if
if [condition] {|
    [expression when true]
|}

# if and else
if [condition] {|
    [expression when true]
|} else {|
    [expression when false]
|}

# if and else if and else
if [condition] {|
    [expression when true]
|} else if [2nd condition] {|
    [expression when true]
|} else {|
    [expression when false]
|}

The [condition] must be a boolean, or an expression resulting in a boolean otherwise it will result in an error. The [expression when true] and [expression when false] are the expression to evaluated when the condition is either true or false.

When there isn't a else block, the if expression will return nothing, as such it cannot be used as a value.

Examples

foo = 5 # store the result of a function `function`
if foo == 5 {|
    "foo equal 5"
|} else {|
    "foo doesn't equal 5"
|} # returns "foo equal 5"

foo = 8
a = if foo == 5 {|
    "foo equal 5"
|} else {|
    "foo doesn't equal 5"
|}

a # return "foo doesn't equal 5"

a = if foo == 5 {|
    "foo equal 5"
|} # throw an error due to assignment with no value 

release

Documentation

Tests

Clone this wiki locally