Skip to content

if

Control word: evaluate expression/s under some condition

Syntax: if[test;e1;e2;e3;…;en]

Where

  • test is an expression that evaluates to an atom
  • e1, e2, … en are expressions

unless test evaluates to zero, the expressions e1 to en are evaluated, in order.

q)a:100
q)r:""
q)if[a>10;a:20;r:"true"]
q)a
20
q)r
"true"

Control word

if is not a keyword (function) but a control word.

It returns Identity (::).

Assigning a local variable within a code branch

if is often preferred to Cond when a test guards a side effect, such as amending a global. But setting local variables using if can have unintended consequences.

Cond, Vector Conditional
Basics: Control