在 LISP 中构造时
在本文中,我们将讨论when构造。 when是用于指定决策的决策语句。它类似于条件语句。
语法:
(when (condition) (statements) )
在哪里,
- 条件是用于测试的测试语句
- 语句是取决于条件的动作
示例 1 :LISP 程序检查数字是否等于 50
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")
)
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")
)
输出:
Equal to 50
示例 2:使用运算符检查给定数字的 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")
)
输出:
Equal to 50
greater than or Equal to 50
less than or Equal to 50