Skip to content

while

Evaluate expressions while some condition remains true

while[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 cycle (evaluate test, then the expressions) continues until test evaluates to zero.

q)r:1 1
q)x:10
q)while[x-:1;r,:sum -2#r]
q)r
1 1 2 3 5 8 13 21 34 55 89
q)i:0
q)while[i<3;i+:1]
q)i
3

The result of while is always the generic null.

while is not a function but a control construct. You cannot iterate or project it.

Name scope

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


Accumulators – While, do, if

Controlling evaluation

Q for Mortals 10.1.7 while