# `if`

_Evaluate expressions under some condition_
```syntax
if[test;e1;e2;e3;…;en]
```

Control construct. Where

- `test` is an expression that evaluates to an atom of integral type, typically a boolean
- `e1`, `e2`, … `en` are expressions

unless `test` evaluates to zero, q evaluates the expressions `e1` to `en` in order.

The result of `if` is always the [generic null](identity.md#null).
```q
q)a:100
q)r:""
q)if[a>10;a:20;r:"true"]
q)a
20
q)r
"true"
```

A null integer is not zero, so it counts as true.
```q
q)if[0N;r:"null"]
q)r
"null"
```

> **Warning — `if` is not a function but a control construct. You cannot iterate or project it.**
>
>
Prefer `if` to [Cond](cond.md) when a test guards a side effect, such as amending a global variable.

A common use is to catch special or invalid arguments to a function.
```q
foo:{[x;y]
  if[type[x]<0; :x];            / no-op for atom x
  if[count[y]<>3; '"length"];   / invalid y
  ..
  }
```

## Name scope

The brackets of the expression list do not create lexical scope.
Name scope within the brackets is the same as outside them.

> **Warning — Setting local variables using `if` can have [unintended consequences](function-notation.md#name-scope).**
>
>
----

[Cond](cond.md),
[`do`](do.md),
[`while`](while.md),
[Vector Conditional](vector-conditional.md)


[Controlling evaluation](control.md)


_Q for Mortals_
[§10.1.5 `if`](../learn/q4m/10_Execution_Control.md#1015-if)
