Syntax patterns: If statements vs ternary expressions in v6
Pine Script supports two primary styles for conditional logic: full if else blocks for multi-step logic and the ternary operator for quick scalar evaluations.
| Pattern | Syntax structure | When to use | Key restriction |
|---|---|---|---|
| Standard if block | if condition\n statement | Executing multi-line actions or order submissions | Requires strict 4-space indentation |
| if else ladder | if c1\n s1\nelse if c2\n s2\nelse\n s3 | Categorizing market regimes into multiple exclusive states | Blocks execute sequentially until first true match |
| Value-returning if | val = if c1\n res1\nelse\n res2 | Assigning complex multi-line calculation outputs to one variable | Every branch must return the same data type |
| Ternary operator | val = condition ? trueVal : falseVal | Plot color selection and simple threshold toggles | Limited to single expressions per branch |