4
If Keyword
DiamondNether90 edited this page 2026-01-09 17:42:40 +11:00

Keyword: if

Syntax: if $bool %line

Average Time Complexity: O(1)

Worst Case Time Complexity: O(n), where n is the number of labels created

Overview

The if keyword allows you to jump to a label or line if a condition is true. It is a core part of the ground programming language, as it would not be Turing Complete without.

Example usage

@label
    if false %label

The second line checks if false is true. Obviously this is false, so nothing happens and program execution continues.

However, consider this code:

@label
    if true %label

The second line checks if true is true. This is true, so the program jumps back to @label, creating an infinite loop.

A more practical example would be to repeat some code a certain amount of times, e.g. 5

set &loop_counter 0
@start_loop
    equal $loop_counter 5 &cond # if loop_counter equals 5, set cond to true, otherwise set cond to false
    if $cond %end_loop # If cond (i.e. if loop_counter equals 5), exit the loop
    # Do some code
    add $loop_counter 1 &loop_counter # Increment loop_counter
    jump %start_loop # Repeat
@exit_loop