📜  如果在 LISP 中构造

📅  最后修改于: 2022-05-13 01:54:22.442000             🧑  作者: Mango

如果在 LISP 中构造

在本文中,我们将讨论 LISP 中的if构造。 if是一个决策语句,用于检查条件是对还是错。如果条件正确,则进入if块内部,执行if块下的语句。否则,不执行语句。

语法

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

在这里, then是 if 语句中使用的可选关键字。

示例 1 :用运算符检查条件的 LISP 程序

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"))


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"))


输出:

equal to 100
greater than 50
less than 150

示例 2:

语言

;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"))

输出:

greater than 50
less than 250