Skip to content

Control Structures

CD2 edited this page Feb 9, 2023 · 3 revisions

If

Executes the following statement or block if the condition is met. else is not allowed.

int a = 1, b = 2, c = 3;

if(a < b) a = b;

if(a == b){

if(a < c) a = b = c;

}

Branches

Executes where the condition is met first.

int a = 1, b = 2, c = 3;

branch {

cond a < b:

a = b;

cond a == b:

else:

break; // leave in the middle with break

comm: // Executed if not broken

if(a < c) a = b = c;

}

branch {

cond<A> a < b: // name the branch A

a = b;

cond<B> a == b: // name the branch B

comm:

if(a >= c) else;// Jump to next else if a >= c

diff<A>: // if A then this is done

diff<B>: // if B then this is done

else:

break;

comm: // Executed if not broken

a = b = c;

}

Switches

Executes constant expressions with the same value.

switch(1+1){

case 2:

// If 1+1 is 2, this is executed

case 1, -1:

// If 1+1 is 1 or -1, this is executed

default:

// If 1+1 is neither -1 nor 1 nor 2, this is executed

case 0:<- // Jump to the next :<- if there is no break in the middle

}

Loop

As long as the condition is satisfied, return to do: and repeat.

int a = 0;

loop {

int n = 1;

while; // Jump to continuation condition test first

do:

if(n % 2 == 0) continue; // jump to continue: if n is even

a += n;

continue:

n++;

while n <= 10:

}

Breaks

Exit the block.

int a = 0;

{

break;

a++;

break: // Everything below is always executed before exiting the block

a--;

}

loop {

do:

loop {

do:

if (a > 0) {

a--;

break, break, do; // You can specify statements to be executed immediately after leaving

}

while a > 0:

breaks:

a = 0; // Once this loop block is entered, a will always be 0 when exiting

}

while a != 0:

}

Lock

The inside of the lock block becomes a critical section due to the mutex of the specified reference type variable. You can specify multiple reference type variables by separating them with ,.

^int a.new(1);

lock(a){

a.new(0); // Even if you dereference here, until you exit the block

// keep the original reference

}

Wait

Waits for the thread to terminate or for the reference type variable's mutex to be released. You can specify multiple variables by separating them with ,.

void f(){}

thread!f? t..();

wait(t); // wait for the thread to finish

Clone this wiki locally