there may be a situation, when you need to execute a block of code numbers of times. a loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages.

lisp provides the following types of constructs to handle looping requirements. click the following links to check their detail.
sr.no. | construct & description |
---|---|
1 |
loop
the loop construct is the simplest form of iteration provided by lisp. in its simplest form, it allows you to execute some statement(s) repeatedly until it finds a return statement. |
2 |
loop for
the loop for construct allows you to implement a for-loop like iteration as most common in other languages. |
3 |
do
the do construct is also used for performing iteration using lisp. it provides a structured form of iteration. |
4 |
dotimes
the dotimes construct allows looping for some fixed number of iterations. |
5 |
dolist
the dolist construct allows iteration through each element of a list. |
gracefully exiting from a block
the block and return-from allows you to exit gracefully from any nested blocks in case of any error.
the block function allows you to create a named block with a body composed of zero or more statements. syntax is −
(block block-name( ... ... ))
the return-from function takes a block name and an optional (the default is nil) return value.
the following example demonstrates this −
example
create a new source code file named main.lisp and type the following code in it −
(defun demo-function (flag) (print 'entering-outer-block) (block outer-block (print 'entering-inner-block) (print (block inner-block (if flag (return-from outer-block 3) (return-from inner-block 5) ) (print 'this-wil--not-be-printed)) ) (print 'left-inner-block) (print 'leaving-outer-block) t) ) (demo-function t) (terpri) (demo-function nil)
when you click the execute button, or type ctrl+e, lisp executes it immediately and the result returned is −
entering-outer-block entering-inner-block entering-outer-block entering-inner-block 5 left-inner-block leaving-outer-block