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

REBOL 3 Functions: case

case  block  /all

Evaluates each condition, and when true, evaluates what follows it.

Arguments:

block [block!] - Block of cases (conditions followed by values)

Refinements:

/all - Evaluate all cases (do not stop at first true case)

See also:

switch   if   either   select   find  

Description

The case function provides a useful way to evaluate different expressions depending on specific conditions. It differs from the switch function because the conditions are evaluated and can be an expression of any length.

The basic form of case is:

case [
    cond1 [code1]
    cond2 [code2]
    cond3 [code3]
]

The if a condition is true (that is, it is not false or none ) then the block that follows it is evaluated, otherwise the next condition is evaluated.

num: 50
case [
    num < 10 [print "small"]
    num < 100 [print "medium"]
    num < 1000 [print "large"] 
]
medium

To create a default case, simply use true as your last condition:

num: 10000
case [
    num < 10 [print "small"]
    num < 100 [print "medium"]
    num < 1000 [print "large"] 
    true [print "huge"]
]
huge

Return Value

The case function will return the value of the last expression it evaluated. This can come in handy:

num: 50
value: case [
    num < 10 [num + 2]
    num < 100 [num / 2]
    true [0]
]
print value
25

Evaluating All

Normally the case function stops after the first true condition is found and its block evaluated. However, the /all option forces case to evaluate the expressions for all true conditions.

num: 50
case/all [
    num < 10 [print "small"]
    num < 100 [print "medium"]
    num < 1000 [print "large"] 
]
medium
large


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