Skip to content

if

Conditional execution based on boolean expressions.

Syntax

tcl
if expr1 ?then? body1 ?elseif expr2 ?then? body2 ...? ?else bodyN?

Parameters

  • expr1: Boolean expression evaluated with expr
  • then: Optional keyword for readability
  • body1: Script to execute if expr1 is true
  • elseif: Keyword introducing additional conditions
  • expr2: Additional condition expression
  • body2: Script to execute if expr2 is true
  • else: Keyword introducing the fallback branch
  • bodyN: Script to execute if no conditions are true

Boolean Values

Conditions can evaluate to:

  • Boolean literals: true, false, yes, no
  • Integer values: 0 is false, any non-zero value is true

Return Value

Returns the result of the executed body script. If no condition matches and no else clause exists, returns an empty string.

Examples

Basic if-else

Output

Multiple conditions with elseif

Output

Using optional then keyword

Output

See Also

  • switch - Multi-way branching
  • expr - Expression evaluation