From J Wiki
Jump to navigation Jump to search

<<   Back to: Vocabulary Thru to: Dictionary

while. condition do. body end. Loop while Control

This flow control structure is similar to the While iteration scheme found e.g. in Ada or Python.
The conditional expression while. … sitting at the top of the loop, enables one to decide whether (and to what extent) the statements inside the do. … end. block are in turn executed.

The code below mimics in part the primitive x #: y (Antibase).

Arbitrary-Base-from-Decimal Conversion of Integers (méthode ancienne);
verb afd takes number;base as (boxed) arguments.

afd=: monad define
 'n b'=. <.|> y           NB. fetch (positive) decimal integer and target base
 if. n < 1 do. 
  v=. 0                   NB. zero is 0 in any base
  v=. i.0                 NB. initialise result vector (to empty)
  while. n > 0 do.
   v=. v,b|n              NB. amend vector with residue
   n=. <. n % b           NB. get next n through integer divide
 |.v                      NB. return digits in target base

Example use:

   afd _122.3;91
1 31
   afd 44252;16
10 12 13 12
whilst. condition do. body end. Execute body once, then loop while Control

Valid only inside an explicit definition.