Sidechair

else

Browse the reference

The fallback branch of an if: it runs when no earlier condition held, and can be written else if to chain tests.

Try it in the editor

else follows an if block and runs when that condition was false. else if tests another condition only when the ones above it failed, so a chain stops at the first match. A final plain else covers every case the conditions did not.

When the if is used as an expression, a missing else leaves the result na on bars where nothing matched, so give the chain an else whenever you need a value on every bar. A switch has no else: its default branch is written as => followed by a result, with no case value before it.

Remarks

  • Indent else to the same column as its if.
  • An if / else if / else chain evaluates its conditions from top to bottom and runs only the first block that matches, so put the narrowest test first.

Example

Pine ScriptThree-way classificationOpen in editor
//@version=6
indicator("Else chain")
body = math.abs(close - open)
range_ = high - low
kind = if range_ == 0
    "flat"
else if body > range_ * 0.7
    "strong"
else
    "weak"
plot(body, "Body", kind == "strong" ? color.green : kind == "weak" ? color.gray : color.blue, style = plot.style_columns)

See also

Runs on Sidechair