REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 13-Aug-2010 Edit History  

REBOL 3 Functions: while

while  cond-block  body-block

While a condition block is TRUE, evaluates another block.

Arguments:

cond-block [block!]

body-block [block!]

See also:

until   loop   repeat   for  

Description

The while function repeats the evaluation of its two block arguments while the first block returns true. The first block is the condition block, the second block is the evaluation block. When the condition block returns false or none!, the expression block will no longer be evaluated and the loop terminates.

The general form is:

while [cond] [body]

For example:

num: 0
while [num < 3] [
    print num
    num: num + 1
]
0
1
2

Another example, using while to traverse a series (like foreach ):

color: [red green blue]
while [not tail? color] [
    print first color
    color: next color
]
red
green
blue

Here is an example using a string series:

str: "abc"
while [not tail? str: next str] [
    print ["length of" str "is" length? str]
]
length of abc is 3
length of bc is 2
length of c is 1

Condition Block

The condition block can contain any number of expressions, so long as the last expression returns the condition. To illustrate this, the next example adds a print to the condition block. This will print the index value of the color. It will then check for the tail of the color block, which is the condition used for the loop.

color: [red green blue]
while [
    print index? color
    not tail? color
][
    print first color
    color: next color
]
1
red
2
green
3
blue
4

Return Value

The last value of the block is returned from the while function.

Other Notes


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin