- LISP - CLOS
- LISP - Error Handling
- LISP - Packages
- LISP - Structures
- LISP - File I/O
- LISP - Input & Output
- LISP - Hash Table
- LISP - Tree
- LISP - Set
- LISP - Vectors
- LISP - Symbols
- LISP - Lists
- LISP - Sequences
- LISP - Strings
- LISP - Arrays
- LISP - Characters
- LISP - Numbers
- LISP - Predicates
- LISP - Functions
- LISP - Loops
- LISP - Decisions
- LISP - Operators
- LISP - Constants
- LISP - Variables
- LISP - Macros
- LISP - Data Types
- LISP - Basic Syntax
- LISP - Program Structure
- LISP - Environment
- LISP - Overview
- LISP - Home
LISP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
LISP - Loops
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. Cpck the following pnks to check their detail.
Sr.No. | Construct & Description |
---|---|
1 | 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 | The loop for construct allows you to implement a for-loop pke iteration as most common in other languages. |
3 | The do construct is also used for performing iteration using LISP. It provides a structured form of iteration. |
4 | The dotimes construct allows looping for some fixed number of iterations. |
5 | The dopst construct allows iteration through each element of a pst. |
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.psp 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 cpck 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-BLOCKAdvertisements