- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 468字
- 2021-06-24 14:21:46
Conditional evaluation
Conditional evaluation means that pieces of code are evaluated, depending on whether a Boolean expression is either true or false. The familiar if...elseif...else...end syntax is used here, which is as follows:
# code in Chapter 4\conditional.jl var = 7 if var > 10 println("var has value $var and is bigger than 10.") elseif var < 10 println("var has value $var and is smaller than 10.") else println("var has value $var and is 10.") end # => prints "var has value 7 and is smaller than 10."
The elseif (of which there can be more than one) or else branches are optional. The condition in the first branch is evaluated, only the code in that branch is executed when the condition is true, and so on; so only one branch ever gets evaluated. No parentheses around condition(s) are needed, but they can be used for clarity. Each expression tested must effectively result in a true or false value, and no other values (such as 0 or 1) are allowed.
Because every expression in Julia returns a value, so also does the if expression. We can use this expression to do an assignment depending on a condition. In the preceding case, the return value is nothing since that is what println returns.
However, in the following snippet, the value 15 is assigned to z:
a = 10 b = 15 z = if a > b a
else b end
These kinds of expression can be simplified using the ternary operator ? (which we introduced in the Recursive functions section in Chapter 3, Functions) as follows:
z = a > b ? a : b
Here, only a or b is evaluated and parentheses ( ) can be added around each clause, as they are necessary for clarity. The ternary operator can be chained, but then it often becomes harder to read. Our first example can be rewritten as follows:
var = 7 varout = "var has value $var" cond = var > 10 ? "and is bigger than 10." : var < 10 ? "and is
smaller than 10" : "and is 10." println("$varout $cond") # var has value 7 and is smaller than 10
Using short-circuit evaluation (refer to the Elementary mathematical functions section in Chapter 2, Variables, Types, and Operations), the statements with if...only are often written as follows:
if <cond> <statement> end is written as <cond> && <statement if !<cond> <statement> end is written as <cond> || <statement>
To make this clearer, the first can be read as <cond> and then <statement>, and the second as <cond> or else <statement>.
This feature can come in handy when guarding the parameter values passed into the arguments, which calculates the square root, like in the following function:
function sqroot(n::Int) n >= 0 || error("n must be non-negative")
n == 0 && return 0 sqrt(n) end sqroot(4) #=> 2.0 sqroot(0) #=> 0.0 sqroot(-6) #=> ERROR: LoadError: n must be non-negative
The error statement effectively throws an exception with the given message and stops the code execution (refer to the Exception handling section in this chapter).
Julia has no switch/case statement, and the language provides no built-in pattern matching (although one can argue that multiple dispatch is a kind of pattern matching that is based not on value, but on type).
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- 深入淺出Electron:原理、工程與實(shí)踐
- Vue.js入門(mén)與商城開(kāi)發(fā)實(shí)戰(zhàn)
- Java EE 7 Development with NetBeans 8
- ASP.NET開(kāi)發(fā)與應(yīng)用教程
- C語(yǔ)言程序設(shè)計(jì)
- Learning Continuous Integration with TeamCity
- Service Mesh實(shí)戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實(shí)踐
- Java零基礎(chǔ)實(shí)戰(zhàn)
- 持續(xù)集成與持續(xù)交付實(shí)戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- C++編程兵書(shū)
- Java EE 7 with GlassFish 4 Application Server
- 例解Python:Python編程快速入門(mén)踐行指南
- Android應(yīng)用開(kāi)發(fā)攻略
- 用Go語(yǔ)言自制編譯器