Open In App

Cond Construct in LISP

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss cond construct in LISP. The cond is a decision-making statement used to make n number of test conditions. It will check all the conditions.

Syntax:

(cond   (condition1 statements)
(condition2 statements)
(condition3 statements)
...
(conditionn statements)
)

Here,

  1. The conditions specify different conditions – if condition1 is not satisfied, then it goes for next condition  IE condition until the last condition.
  2. The statements specify the work done based on the condition.

Note: It will execute only one statement.

Example 1: LISP program to check whether a number is greater than 200 or not

Lisp




;set value1 to 500
(setq val1 500)
  
;check whether the val1 is greater than 200
(cond ((> val1 200)
   (format t "Greater than 200"))
   (t (format t "Less than 200")))


Output:

Greater than 200

Example 2: Demo with comparison operators

Lisp




;set value1 to 500
(setq val1 500)
  
;check whether the val1 is greater  than 200
(cond ((> val1 200)
   (format t "Greater than 200"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 500
(cond ((= val1 500)
   (format t "equal to 500"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is equal to 600
(cond ((= val1 600)
   (format t "equal to 500"))
   (t (format t "Not")))
 (terpri)
   
 ;check whether the val1 is greater than or equal to 400
(cond ((>= val1 400)
   (format t "greater than or equal to 400"))
   (t (format t "Not")))
     
 (terpri)
   
;check whether the val1 is less than or equal to 600
(cond ((<= val1 600)
   (format t "less than or equal to 600"))
   (t (format t "Not")))


Output:

Greater than 200
equal to 500
Not
greater than or equal to 400
less than or equal to 600


Last Updated : 09 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads