SultanC
  1. Home
  2. Examples
  3. Errors

`assert` stops the program with exit code 1 when a condition is false

errors/assertions.sn
// `assert` stops the program with exit code 1 when a condition is false.
import std.io.println;
import std.debug.assert;

def factorial(number: int): int {
    if (number <= 1) { return 1; };
    return number * factorial(number - 1);
}

def main(): int {
    assert(factorial(5) == 120);
    let message: string = "factorial(6) must be 720";
    assert_with_message(factorial(6) == 720, &message);
    println("all checks passed");
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/errors/assertions.sn

Or compile it to an executable:

./build/sultanc examples/en/errors/assertions.sn -o build/example && ./build/example

Source on GitHub   All examples