Statement

Statements is a code which using to control variable how to operate, and what to operate.
The statements in Blackey Script also learn from C++ syntax.

In general, most proportions of code are composed by expression statements.
Expression statements are executed in order from top to bottom.
It may jump when it encounters other statements such as selection / iteration / jump statements.

Those statements are listed in the following table:

Statements
Expression statement
Selection statement
Iteration statement
Jump statement

Next, we will expand on each of the different statements:

Expression statement

The syntax of expression statement is very simple, notice that the square brackets need to be ignored.

[expression ] ;

Expression statements are executed in order.
The most common expression statements, such as assignment / call functions, are as follows:

x = 1;
y = 1 * 2;
print(y);

Selection statement

Currently Blackey Script only supports the “IF-ELSE” statements in select statements.
“IF-ELSE” statements using expression statement as conditions to control branches,
when the expression gets a non-zero value, it will jump into the “IF” block and skip other blocks.
If the value of the expression returns zero, it will skip the “IF” block and jump to the “ELSE” block if there has an “ELSE” block.

Lets see some examples:

Case 1. only IF

// If the expression returns non-zero value, then it will enter the “IF” block and execute the statements (statements1; …)
if ( expression ) {

statement1;

}

Case 2. IF-ELSE

// If the expression returns non-zero value, then it will enter the “IF” block and execute the statements (statements1; …)
// And ignore the “ELSE” block

if ( expression ) {

statement1;

} else {

// If the expression returns zero, then it will enter the “ELSE” block and execute the statements (statements2; …)
// The “IF” block will be ignored.

statement2;

}

Case 3. IF-ELSE-IF

// If the expression1 returns non-zero value, then it will enter the “IF” block and execute the statements (statements1; …)
// And ignore other blocks (“ELSE-IF” and “ELSE” block)

if ( expression1 ) {

statement1;

} else if (expression2) {

// If the expression1 returns zero, and expression2 returns non-zero value,
// it will enter the “ELSE-IF” block and execute the statements (statements2; …)
// And ignore other blocks (“IF” and “ELSE” block)

statement2;

} else {

// When both expression1 and expression2 returns zero, execute the “ELSE” block (statement3; …)
statement3;

}

Here is an exercise for you:

x = 1;
y = 2;
if (x < y) {

print(“x < y”);

} else {

print(“x >= y”);

}
// Print “x < y”

Iterator statement

Iteration statements cause statements (or compound statements) to be executed zero or more times,
subject to some loop-termination criteria.
When these statements are compound statements, they are executed in order,
except when either the break statement or the continue statement is encountered.

Currently Blackey Script only support the “WHILE” statement for iteration statements,
the syntax is as below:

while ( expression ) {

statement;

}

Here is an example:

x = 0;
while (x < 5) {

print(x);
x = x + 1;

}
// Print 0, 1, 2, 3, 4

Jump statement

Jump statements cause an unconditional jump to another statement elsewhere in the code.
They are used primarily to interrupt the loops or self-defined functions.
Blackey Script support three kinds of jump statements:

Jump statements
break; The break statement terminates execution of the immediately enclosing while statement.
continue; The continue statement passes control to the end of the immediately enclosing while statement.
return [expression] ; The return statement terminates execution of a function and returns control to the calling function,
with or without a return value.

break / continue

break/continue statements usually composed with the IF-ELSE statements to decide whether it need to break out from a loop,
as below:

BREAK case.

x = 0;
while (x < 100) {

print(x);
x = x + 1;
if (x > 5) {

print(“break out”);
break;

}

}
print(“done”);
// Print 0, 1, 2, 3, 4, 5, “break out”, “done”

CONTINUE case.

x = 0;
while (x < 100) {

x = x + 1;
if (x > 5) {

continue;

}
print(x);

}
print(“done”);
// Print 1, 2, 3, 4, 5, “done”

return

return statements is usually using in the self-defined function.
If you use return statement directly in your main script,
it will terminate the whole script while execute the return statement.

x = 0;
while (x < 100) {

print(x);
x = x + 1;
if (x > 5) {

print(“break out”);
return 0;

}

}
print(“done”);
// Print 0, 1, 2, 3, 4, 5, “break out”
// Different from the example of BREAK, it won’t print the “done” string here.

exit

exit statement is usually using in the self-defined function.
It will terminate whole script while execute the exit statement.
If you calling exit with a non-zero value, it will print a log to the Log Window,
which can let you check where is the exit point easier.

def quitCheck(val) {

if (val == 3) {

exit(1);

}

}

x = 0;
while (x < 100) {

x = x + 1;
quitCheck(x);
print(x);

}
print(“done”);
// Print 1, 2, 3, 4

// Print exit(1)
[Line: 13] exit error code 1.000000