Control Flow

You will know about all the control flow & state in Pa in this chapter.

if Statements

If statements take an expression which represents the condition, if it evaluates to true then its body will be executed, otherwise it will either jump and skip over its body and continue the code or jump to an else clause if provided.


if true { //true
    print("yes"); //executed.
}

if false { //false
    print("yes"); //skipped.
} else {
    print("no"); //executed.
}
                    

Loops

Pa features only 2 kind of loops, and helper one as built-in method for the list and number objects.

For loops

For loops take 3 clauses, all of them are optional. The first clause can be a local variable, the second clause is the condition and the third is an expression representing incrementing.


//i++ is a shorthand for i = i + 1.                        
for let i = 0; i <= 5; i++ {
    //prints numbers from 0 to 5.
    print(i);
}

for ;;; {
    //this loop executes forever
    print("infinite.");
}
                    

While loops

Similarly to if statements, while loops only take 1 expression which represents the condition, as long as the condition evaluates to true the while loop will keep executing its body.


while true {
    //this loop executes forever.
    print("infinite");
}

let i = 0;
while i <= 5 {
    //prints numbers from 0 to 5.
    print(i);
    i++;
}
                    

Continue statements

The continue statement is used to skip an iteration inside loop. It can be only used inside the loop statements previously mentioned.


for let i = 0; i < 5; i++ {
    if i == 3 {
        //skip this iteration.
        continue;
    }

    //prints 0, 1, 2, 4.
    print(i);
}
                    

Break statements

The break statement is used to stop a loop. It can be only used inside the loop statements previously mentioned.


for let i = 0; i < 5; i++ {
    if i == 3 {
        //stop this loop.
        break
    }

    //prints 0, 1, 2, 3.
    print(i);
}

while true {
    //prints once.
    print("once");
    break;
}
                    

Assert statements

The assert statement check if it's condition is false, if so, it will raise a Runtime error taking the optional error message string.


assert false, "Runtime Error!";

//Only condition.
assert false;

assert [1,2,3].length() == 2, 
    "List must contain only 2 items!";