当前位置:K88软件开发文章中心编程语言非主流编程语言Julia → 文章内容

Julia 控制流

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 16:28:09

由 陈 创建,youj 最后一次修改 2016-08-12 控制流 Julia 提供一系列控制流: 复合表达式 : begin 和 (;) 条件求值 : if-elseif-else 和 ?: (ternary operator) 短路求值 : &&, || 和 chained comparisons 重复求值: 循环 : while 和 for 异常处理 : try-catch , error 和 throw 任务(也称为协程) : yieldto 前五个控制流机制是高级编程语言的标准。但任务不是:它提供了非本地的控制流,便于在临时暂停的计算中进行切换。在 Julia 中,异常处理和协同多任务都是使用的这个机制。 复合表达式 用一个表达式按照顺序对一系列子表达式求值,并返回最后一个子表达式的值,有两种方法:begin 块和 (;) 链。 begin 块的例子:julia> z = begin x = 1 y = 2 x + y end3 这个块很短也很简单,可以用 (;) 链语法将其放在一行上:julia> z = (x = 1; y = 2; x + y)3 这个语法在函数中的单行函数定义非常有用。 begin 块也可以写成单行, (;) 链也可以写成多行:julia> begin x = 1; y = 2; x + y end3julia> (x = 1; y = 2; x + y)3 条件求值 一个 if-elseif-else 条件表达式的例子:if x < y println("x is less than y")elseif x > y println("x is greater than y")else println("x is equal to y")end 如果条件表达式 x < y 为真,相应的语句块将会被执行;否则就执行条件表达式 x > y ,如果结果为真,相应的语句块将被执行;如果两个表达式都是假,else 语句块将被执行。这是它用在实际中的例子:julia> function test(x, y) if x < y println("x is less than y") elseif x > y println("x is greater than y") else println("x is equal to y") end endtest (generic function with 1 method)julia> test(1, 2)x is less than yjulia> test(2, 1)x is greater than yjulia> test(1, 1)x is equal to y elseif 及 else 块是可选的。 请注意,非常短的条件语句(一行)在 Julia 中是会经常使用短的电路评估(Short-Circuit Evaluation)实现的,具体细节在下一节中进行概述。 如果条件表达式的值是除 true 和 false 之外的值,会出错:julia> if 1 println("true") endERROR: type: non-boolean (Int64) used in boolean context “问号表达式”语法 ?: 与 if-elseif-else 语法相关,但是适用于单个表达式:a ? b : c ? 之前的 a 是条件表达式,如果为 true ,就执行 : 之前的 b 表达式,如果为 false ,就执行 : 的 c 表达式。 用问号表达式来重写,可以使前面的例子更加紧凑。先看一个二选一的例子:julia> x = 1; y = 2;julia> println(x < y ? "less than" : "not less than")less thanjulia> x = 1; y = 0;julia> println(x < y ? "less than" : "not less than")not less than 三选一的例子需要链式调用问号表达式:julia> test(x, y) = println(x < y ? "x is less than y" : x > y ? "x is greater than y" : "x is equal to y")test (generic function with 1 method)julia> test(1, 2)x is less than yjulia> test(2, 1)x is greater than yjulia> test(1, 1)x is equal to y 链式问号表达式的结合规则是从右到左。 与 if-elseif-else 类似,: 前后的表达式,只有在对应条件表达式为 true 或 false 时才执行:julia> v(x) = (println(x); x)v (generic function with 1 method)julia> 1 < 2 ? v("yes") : v("no")yes"yes"julia> 1 > 2 ? v("yes") : v("no")no"no" 短路求值 && 和 || 布尔运算符被称为短路求值,它们连接一系列布尔表达式,仅计算最少的表达式来确定整个链的布尔值。这意味着: 在表达式 a && b 中,只有 a 为 true 时才计算子表达式 b在表达式 a || b 中,只有 a 为 false 时才计算子表达式 b&& 和 || 都与右侧结合,但 && 比 || 优先级高:julia> t(x) = (println(x); true)t (generic function with 1 method)julia> f(x) = (println(x); false)f (generic function with 1 method)julia> t(1) && t(2)12truejulia> t(1) && f(2)12falsejulia> f(1) && t(2)1falsejulia> f(1) && f(2)1falsejulia> t(1) || t(2)1truejulia> t(1) || f(2)1truejulia> f(1) || t(2)12truejulia> f(1) || f(2)12false 这种方式在 Julia 里经常作为 if 语句的一个简洁的替代。 可以把 if <cond> <statement> end 写成 <cond> && <statement> (读作 <cond> *从而* <statement>)。 类似地, 可以把 if ! <cond> <statement> end 写成 <cond> || <statement> (读作 要不就 )。 例如, 递归阶乘可以这样写:julia> function factorial(n::Int) n >= 0 || error("n must be non-negative") n == 0 && return 1 n * factorial(n-1) endfactorial (generic function with 1 method)julia> factorial(5)120julia> factorial(0)1julia> factorial(-1)ERROR: n must be non-negative in factorial at none:2 非短路求值运算符,可以使用数学运算和基本函数中介绍的位布尔运算符 & 和 | :julia> f(1) & t(2)12falsejulia> t(1) | t(2)12true && 和 || 的运算对象也必须是布尔值( true 或 false )。在任何地方使用一个非布尔值,除非最后一个进入连锁条件的是一个错误:julia> 1 && trueERROR: type: non-boolean (Int64) used in boolean context 另一方面,任何类型的表达式可以使用在一个条件链的末端。根据前面的条件,它将被评估和返回:julia> true && (x = rand(2,2))2x2 Array{Float64,2}: 0.768448 0.673959 0.940515 0.395453julia> false && (x = rand(2,2))false 重复求值: 循环 有两种循环表达式: while 循环和 for 循环。下面是 while 的例子:julia> i = 1;julia> while i <= 5 println(i) i += 1 end12345 上例也可以重写为 for 循环:julia> for i = 1:5 println(i)

[1] [2] [3] [4]  下一页


Julia 控制流