Skip to content

If / Else

If Statements

If statements use the if keyword followed by a condition wrapped in parentheses.

if (condition) {
    // ...code
}

For information on how to write conditions, see Conditional Expressions. For basic conditions, use the tool below.

Expand this box to convert If Actions to their Terracotta equivalents

Click on "If Player" to change to a different code block. Loading...

Inverting (NOT arrow equivalent)

To invert the entirety of an if statement's condition, an exclamation point ! can be placed in front of the condition's parentheses.

// these both do the exact same thing
if !(condition) {}
if (!condition) {}

// these both do the exact same thing
if !(a && b) {}
if (!(a && b)) {}

Else Statements

Else statements can be placed immediately after an if statement using the else keyword.

if (condition) {
    // ...code
} else {
    // ...other code
}

Else-If Statements

Else if statements are also supported. They must be placed after the if statement but before the final else statement.

You can have as many else if statements as you want and conditions can be arbitrarily complex.

if (default.xpLevel > 10 || default.hasPlotPermission(permission="Owner")) {
    // ...code
} else if (num.random(1,10) < 5) {
    // ...code
} else if (default.mainHandItem == item("emerald") && default.isSneaking()) {
    // ...code
} else {
    // ...code
}