Clojure提供了" case" 语句,该语句类似于Java编程语言中可用的" switch" 语句。
case expression value1 statement #1 value2 statement #2 valueN statement #N statement #Default
以下是Clojure中" case"语句的示例。
(ns clojure.examples.hello (:gen-class)) ;; This program displays Hello Learnfk (defn Example [] (def x 5) (case x 5 (println "x is 5") 10 (println "x is 10") (println "x is neither 5 nor 10"))) (Example)
在上面的示例中,我们首先将变量" x"初始化为值5。然后有一个" case"语句,该语句判断变量" x"的值。基于变量的值,它将执行相关的case语句集。如果未执行任何先前的语句,则最后一条语句为默认语句。
上面的代码产生以下输出。
x is 5
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)