Skip to content

If

if {condition}
if {condition} {lines}

Also searchable as: If

if runs the line or block after it only when the condition is true; otherwise the script skips it. An else after it runs instead when the condition is false.

Parameter Meaning
condition An expression, such as %a > %b or #FindCnt = 0.
lines Optional. Without a block, how many of the following lines belong to the if. Default: 1.

The condition is true only when it evaluates to #true, which is -1. Comparisons (=, <, in, …) produce -1 or 0, and &&, || and ! combine those as expected. A plain value is not turned into true or false: if 1 and if yes are both false, and if %flag is true only when %flag holds #true. See #True and #False.

Without a block, the if controls only the next line:

set %a 100
set %b #random
if %a > %b
display ok a is bigger than b
else
display ok a is equal to or less than b
halt

To control several lines, put them in a block:

set %a 100
set %b #random
if %a > %b
{
display ok a is bigger than b
set %b %a
}
else
{
display ok b is equal to or bigger than a
set %a %b
}
halt

A block is a { line, the lines to run, and a matching } line. The { has to be the very next line: a blank line or a comment in between means only that line belongs to the command, and the block after it runs every time. Braces go on lines of their own.

The older form gives the number of lines instead of a block. Every script line counts, blank lines and comments included:

set %a 100
set %b #random
if %a > %b 2
display ok a is bigger than b
set %b %a
else 2
display ok b is equal to or bigger than a
set %a %b
halt

! reverses only the value right after it, before any comparison is made. So if ! %var = world does not mean “if %var is not world”: it compares ! %var with world, which is never equal. Use <>, or put the condition in parentheses:

set %var hello
if %var <> world
display ok shown: hello is not world
if ! ( %var = world )
display ok shown as well
if ! %var = world
display ok never shown
halt

if and else can be nested to any depth:

set %a #random % 3 + 1
if %a = 1
display ok One
else
{
if %a = 2
display ok Two
else
display ok Three
}
halt

See also

Based on the EasyUO wiki, edited for EasyUO Reforged. This text is available under the GNU Free Documentation License 1.2.