Decision Making in LISP

• Last Updated : 24 Dec, 2021

Decision making is used to specify the condition to evaluate an expression in LISP.

There are 4 types of decision-making statements in LISP. They are

• if
• cond
• when
• case

if Statement

The if is a decision-making statement used to check whether the condition is right or wrong. The if the condition is right, then it will go inside the if block and execute the statements under if block. Otherwise, the statements are not executed.

Syntax:

(if (condition) then (statement 1).....(statement  n))

Here, then is an optional keyword used inside the if statement.

Example 1: LISP Program to check conditions with operators

Lisp

 ;define value to 100(setq val1 100) ;check the number is equal to 100(if (= val1 100)   (format t "equal to 100"))    (terpri) ;check the number is greater than to 50(if (> val1 50)   (format t "greater than 50"))   (terpri) ;check the number is less than to 150(if (< val1 150)   (format t "less than 150"))

Output:

equal to 100
greater than 50
less than 150

Example 2:

Lisp

 ;define value to 230(setq val1 230) ;check the number is equal to 100(if (= val1 100)   (format t "equal to 100"))    (terpri) ;check the number is greater than to 50(if (> val1 50)   (format t "greater than 50"))   (terpri) ;check the number is less than to 150(if (< val1 250)   (format t "less than 250"))

Output:

greater than 50
less than 250

cond Statement

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,

• The conditions specify different conditions – if condition1 is not satisfied, then it goes for the next condition  IE condition until the last condition.
• 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 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

when Statement

The when is a decision-making statement used to specify the decisions. It is similar to conditional statements.

Syntax:

(when (condition) (statements) )

where,

1. condition is a test statement used to test
2. statements are the actions that will depend on the condition

Example 1: LISP Program to check the number is equal to 50 or not

Lisp

 ;set number to 50(setq number 50) ;condition check the given number is equal to 50(when (= number 50) ;statement  (format t "Equal to 50")   )

Output:

Equal to 50

Example 2: LISP Program to check the given number with comparison operators

Lisp

 ;set number to 50(setq number 50) ;condition check the given number is equal to 50(when (= number 50) ;statement  (format t "Equal to 50")   )(terpri);set number to 150(setq number 150) ;condition check the given number is greater than or equal to 50(when (>= number 50) ;statement  (format t "greater than or Equal to 50")   )(terpri)    ;set number to 10(setq number 10) ;condition check the given number is less than or equal to 50(when (<= number 50) ;statement  (format t "less than or Equal to 50")   )

Output:

Equal to 50
greater than or Equal to 50
less than or Equal to 50

case Statement

This is used to check multiple test conditions at a time, unlike cond, if and when it allows multiple conditions.

Syntax:

(case  (key_value)
((key1)   (statement 1 .............. statement n) )
((key2)   (statement 1 .............. statement n) )
................
((keyn)   (statement 1 .............. statement n) )

here,

1. key_value is the numeric value taken a input
2. keys are the different conditions to test particular condition specified  in keys

Example: LISP Program to get the particular number when the number is given

Lisp

 ;define value to 2(setq val1 2) ;define 5 cases from 1 to 5(case val1(1 (format t "you selected number 1"))(2 (format t "you selected number 2"))(3 (format t "you selected number 3"))(4 (format t "you selected number 4"))(5 (format t "you selected number 5")))

Output:

you selected number 2

Example: LISP Program to perform an arithmetic operation when a particular key is chosen

Lisp

 ;define value1 to 10(setq val1 10) ;define value2 to 20(setq val2 20) ;set input to 1(setq input 1) ;define 4 cases to perform each arithmetic operation(case input ;condition to perform addition(1 (print (+ val1 val2))) ;condition to perform subtraction(2 (print (- val1 val2))) ;condition to perform multiplication(3 (print (* val1 val2))) ;condition to perform division(4 (print (/ val1 val2))))

Output:

30

Set input to 3

Lisp

 ;define value1 to 10(setq val1 10) ;define value2 to 20(setq val2 20) ;set input to 3(setq input 3) ;define 4 cases to perform each arithmetic operation(case input ;condition to perform addition(1 (print (+ val1 val2))) ;condition to perform subtraction(2 (print (- val1 val2))) ;condition to perform multiplication(3 (print (* val1 val2))) ;condition to perform division(4 (print (/ val1 val2))))

Output:

200

My Personal Notes arrow_drop_up