Skip to main content
FW version: Stable

Conditions

info
  • Code blocks and functions are first uploaded to MCU memory and then executed.
  • Other code is executed line by line.

if <expression> <command(s)>

This command evaluates the given expression and, if it yields true, executes the following <command(s)> part. <command(s)> can also be a whole block of commands (beginning with { and ending with }). Command if could be followed by command elif or else.

The expression is C-style string format which can contain braces, operators, variables, and constants. The expression is evaluated in a floating-point scalar and considered valid when the result is nonzero. If the bitwise operators are used in the expression, it is evaluated in a 32-bit fixed-point scalar.

note
  • The nested command or code block gets executed only if the latest if or elif did not result into execution.

  • Note that the if-elif-else can be executed outside a code block. Note that a consecutive branching, i.e. to perform the conditional test again might not be working, since branch command operates only within a code block.

  • Note that since expressions in YOS are always evaluated in certain datatype, which is, on the contrary to 'set' command, not explicitly known in the case of conditional execution commands, the if and elif tries to guess the evaluation type, similarly like 'stringify'.

  • Constants can't be used to guess the data type, thus, e.g:" if (1,2>2,3) will not compare the whole array constant 1,2 against 2,3" but only it's first member, because the default datatype for conditional execution commands is int32.

    echo var x.2
    echo set x 1,2
    echo if (x>2,3)

    Can sucessssfuly determine the evaluation datatype from x variable, thus, the entire array will be compared. Similarly :

    echo if (1.5 > 1.4)

    Will not recognize the floating point type, since there is no variable present in the expression. But :

    echo var f float
    echo set f 1.5
    echo if (f>1.4)

    Can sucessssfuly determine the evaluation datatype from f variable, echo thus, it will be evaluated in float and work as expected.

The operators can be:

  • * - multiplication
  • : - for division
  • + - addition
  • - - subtraction
  • > - greater than
  • < - less than
  • >= - greater or equal
  • <= - less or equal
  • == - equal
  • != - not equal
  • && - logical AND
  • || - logical OR
  • & - bitwise AND
  • | - bitwise OR
  • ^ - bitwise XOR

Expression is true if the value is non-zero and false if zero. The expression is evaluated from the left to the right and operators are not prioritized. If a priority is required, the user must specify that with braces. 3+5*4 needs to be entered as 3+(5*4).


else <command(s)>

Works only if used right after if or elif. When the condition of if (or elif) is true, all command(s) in argument <command(s)> are skipped. When the condition is false, executes <command(s)>. Similar to if, single command in the argument can be replaced with a whole block of commands, beginning with { and ending with }.


elif <expression> <command(s)>

This command is used for chained conditional execution. Works only if used right after if or another elif. When the condition of if (or elif) is true, all command(s) in argument <command(s)> are skipped. When the condition of the previous if or elif is false, executes the argument only if the given <expression> evaluates to true. Similar to if, single command in the argument can be replaced with a whole block of commands, beginning with { and ending with }.

Example1

var max i a.10 int
set i 0
set max 0

# now fill the array ...
set a.0 5
set a.1 2
set a.2 15
# ...

{
if (a.$i<max) set max a.$i
set i i+1
if (i<10) branch s
}
echo max element is $max

Result:

max element is 0

Example2

@!
@c

echo Example: does the if-elif-else construct work?
echo (*) outside a code block

if 1 echo yes
elif 1 echo no
else echo no

if 0 echo no
elif 0 echo no
else echo yes

if 1 { echo yes }
elif 1 { echo no }
else { echo no }

if 0 { echo no }
elif 0 { echo no }
else { echo yes }

echo (*) combined inside-outside code block

if 0 {
if 1 {
echo no
}
else {
echo hell no
}
}
elif 0 {
if 0 {
echo hell no
}
else {
echo no
{
echo hell no
}
}
}
else echo yes

echo You should now see only multiple yes.
echo
echo "Conditional execution commands will return nonzero if the"
echo "corresponding nested command or code block gets executed."
echo

if 1 echo true
echo if 1 returns $retval
if 0 echo false
echo if 0 returns $retval

Result:

Example: does the if-elif-else construct work?
(*) outside a code block
yes
yes
yes
yes
(*) combined inside-outside code block
yes
You should now see only multiple yes.

"Conditional execution commands will return nonzero if the"
"corresponding nested command or code block gets executed."

true
if 1 returns 1
if 0 returns 0