Conditions
A conditional statement allows to evaluate one or more conditional instructions. Depending on the result, a block of code can be either executed or skipped. Conditional statements can be found in nearly all programming languages. It is an important part of the control flow.
Single Condition
A conditional statement starts with an IF keyword and ends with an END keyword.
Syntax
if
<condition>
then
<consequent>
else
<alternative>
end<condition> - a conditional instruction. This instruction turns the condition result into either true or false.
<consequent> - a branch of the code that executes if the condition result is true.
<alternative> - an optional branch of the code that executes if the condition result is false. If else is omitted the flow skips the consequent branch and continues after a closing end keyword.
Example
if
is_player_playing 0
then
0ace: "player is ready"
else
0ace: "player is not ready"
endDepending on the result of the is_player_playing instruction, one or another message will be displayed.
Multiple Conditions (AND)
if and allows to combine up to 8 conditional instructions and execute a block of code if all of them are true.
Syntax
if and
<condition 1>
<condition 2>
...
<condition 8>
then
<consequent>
else
<alternative>
end<consequent> - a branch of the code that executes if all conditions are true.
<alternative> - an optional branch of the code that executes if any condition is false. If else is omitted the flow skips the consequent branch and continues after a closing end keyword.
Short-circuit evaluation is not supported. All conditions are evaluated regardless of the result of the preceding checks.
Example
Checking if a number is within the given range:
int x
if and
x >= 0
x <= 10
then
0ace: "x is between 0 an 10"
endMultiple Conditions (OR)
if or allows to combine up to 8 conditional instructions and execute a block of code if at least one of them is true.
Syntax
if or
<condition 1>
<condition 2>
...
<condition 8>
then
<consequent>
else
<alternative>
end<consequent> - a branch of the code that executes if any condition is true.
<alternative> - an optional branch of the code that executes if all conditions are false. If else is omitted the flow skips the consequent branch and continues after a closing end keyword.
Short-circuit evaluation is not supported. All conditions are evaluated regardless of the result of the preceding checks.
Example
Checking if the vehicle model matches one of the given choices:
if or
model == #PCG600
model == #FREEWAY
then
0ace: "model is either PCG600 or Freeway"
else
0ace: "model is neither PCG600 nor Freeway"
endLast updated