> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Branching

> if, else if, else and switch as statements, and which calls may and may not be placed inside a branch.

`if` and `switch` choose which statements run on a bar. Both are statements with no value, so `x = if ...` is a syntax error; to choose
a value use `c ? a : b` (see [Operators and expressions](https://gocharting.com/docs/scripting/language/expressions.md)).

## if, else if, else

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Candle label", overlay=true)
if barstate.islast {
    if close > open {
        label.new(bar_index, high, "Up", color=color.green)
    } else if close < open {
        label.new(bar_index, low, "Down", color=color.red)
    } else {
        label.new(bar_index, high, "Flat")
    }
}
```

The condition is a `bool` or a number (zero is false, and so is `na`). A string is refused: `if` conditional expression must be of a bool
type!. Braces and line breaks follow [Statements and lines](https://gocharting.com/docs/scripting/language/statements-and-lines.md). A variable declared inside a
block is not visible after it, so declare it before the `if` and assign it with `:=`.

## switch

`switch value {` is followed by `case x:` lines and an optional `default:`.

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Color by phase")
int phase = bar_index % 3
color c = color.gray
switch phase {
    case 0:
        c := color.red
    case 1:
        c := color.green
    default:
        c := color.blue
}
plot(close, color=c)
```

* The first matching case runs and the `switch` ends: there is no fall-through and no `break` is needed.
* A `case` takes exactly one value (`case 0, 1:` is a syntax error). Strings and enum members work as values; the value must have the
  type of the subject (a `float` subject takes an `int` case).
* At most one `default`: Only one `default` case is allowed in a switch statement.

## What may go inside a branch

| Call | Inside `if`, `switch`, loops, functions |
| - | - |
| `label.new`, `line.new`, `box.new` and the other drawings | allowed |
| `alert(message)` | allowed |
| `strategy.entry`, `strategy.exit`, `strategy.close`, ... | allowed |
| `plot`, `hline`, `fill`, `bgcolor` and the rest of the plot family (listed in [Script anatomy](https://gocharting.com/docs/scripting/language/anatomy.md)) | refused |

The refused calls draw one series for the whole script, so they belong at the top level:

```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Plot in a branch")
if close > open {
    plot(close)
}
```

The compiler says: `plot()` must be called in the global scope. The editor's check reports it, and the name in the message is the call you wrote.
Put the condition into an argument and give `na` for the bars you want empty: `plot(close > open ? close : na, title="Up close")`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.