2.6 Conditional execution

2.6.1 Reference pages

when Conditional Execution Clause Keyword

if Conditional Execution Clause Keyword

unless Conditional Execution Clause Keyword

Syntax: {when|if} expr clause1 [and clause]* [else clause2 [and clause]*]

Syntax:unless expr clause1 [and clause]* [else clause2 [and clause]*]

The constructswhen andif allow you to execute loop clauses conditionally. These constructs are synonyms and can be used interchangeably.

If the value of the test expression expr is non-nil, the expression clause1 is evaluated. If the test expression evaluates tonil and anelse construct is specified, the statements that follow theelse are evaluated; otherwise, control passes to the next clause.

Theunless construct is equivalent towhen (not expr) andif (not expr). If the value of the test expression expr isnil, the expression clause1 is evaluated. If the test expression evaluates to non-nil and anelse construct is specified, the statements that follow theelse are evaluated; otherwise, control passes to the next clause.

The clause arguments must be either accumulation, unconditional, or conditional clauses. See Section 2.1 on page 11 for more information.

You can group clauses that follow the test expression by using the loop keywordand to produce a compound clause.

You can use the loop keywordit to refer to the result of the test expression in a clause. If multiple clauses are connected withand, theit construct must be used in the first clause in the block. Sinceit is a loop keyword, you cannot useit as a local variable within a loop.

Ifwhen orif clauses are nested, eachelse is paired with the closest precedingwhen orif construct that has no associatedelse.

;; Group conditional clauses into a block without using 
;; BLOCK or PROGN.
> (loop for i in numbers-list
       when (oddp i)
        do (print i)
        and collect i into odd-numbers
        and do (terpri)
       else                              ; I is even.
        collect i into even-numbers
       finally
        (return (values odd-numbers even-numbers)))

;; Collect numbers larger than 3. > (loop for i in '(1 2 3 4 5 6) when (and (> i 3) i) collect it) ; IT refers to (and (> i 3) i). (4 5 6)

;; Find a number in a list. > (loop for i in '(1 2 3 4 5 6) when (and (> i 3) i) return it) 4

;; The above example is similar to the following one. > (loop for i in '(1 2 3 4 5 6) thereis (and (> i 3) i)) 4

;; Nest conditional clauses. > (loop for i in list when (numberp i) when (bignump i) collect i into big-numbers else ; Not (bignump i) collect i into other-numbers else ; Not (numberp i) when (symbolp i) collect i into symbol-list else ; Not (symbolp i) (error "found a funny value in list ~S, value ~S~%" list i))


The Loop Facility - 9 SEP 1996

Generated with Harlequin WebMaker