- LISP 教程
- LISP - 主頁
- LISP - 概述
- LISP - 環境
- LISP - 程式結構
- LISP - 基本語法
- LISP - 資料型別
- LISP - 宏
- LISP - 變數
- LISP - 常量
- LISP - 運算子
- LISP - 決策
- LISP - 迴圈
- LISP - 函式
- LISP - 斷言
- LISP - 數字
- LISP - 字元
- LISP - 陣列
- LISP - 字串
- LISP - 序列
- LISP - 列表
- LISP - 符號
- LISP - 向量
- LISP - 集合
- LISP - 樹
- LISP - 雜湊表
- LISP - 輸入和輸出
- LISP - 檔案輸入/輸出
- LISP - 結構
- LISP - 包
- LISP - 錯誤處理
- LISP - CLOS
- LISP 實用資源
- Lisp - 快速指南
- Lisp - 實用資源
- Lisp - 討論
Lisp - when 結構
when 宏後面是一個 test 子句,該子句可計算為 t 或 nil。如果 test 子句計算為 nil,則不計算任何形式並返回 nil,但如果測試結果為 t,則執行 test 子句後面的 action。
when 宏的語法 −
(when (test-clause) (<action1) )
示例
建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。
main.lisp
; set a as 100 (setq a 100) ; check if a is greater than 20 (when (> a 20) ; print the statement is above statement is true (format t "~% a is greater than 20")) ; print the statement (format t "~% value of a is ~d " a)
輸出
當你單擊“執行”按鈕,或鍵入 Ctrl+E 時,LISP 立即執行它,返回的結果是 −
a is greater than 20 value of a is 100
示例
更新檔案 main.lisp,並在其中鍵入以下程式碼。
main.lisp
; set a as 10 (setq a 10) ; check if a is greater than 20 (when (> a 20) ; print the statement is above statement is true (format t "~% a is greater than 20")) ; print the statement (format t "~% value of a is ~d " a)
輸出
當你單擊“執行”按鈕,或鍵入 Ctrl+E 時,LISP 立即執行它,返回的結果是 −
value of a is 10
lisp_decisions.htm
廣告