基本格式
if 条件
语句
end
条件是一个值或表达式,结果必须是Bool
类型
else
扩展:
if 条件
语句
else
语句
end
elseif
扩展:
if 条件
语句
elseif 条件
语句
end
示例(还记得如何运行文件吗):
println("输入一行一个整数")
x=parse(Int,readline()) # parse可以把字符串转化为整数
if x>0
print(">0")
elseif x==0
print("=0")
else
print("<0")
end
一种简易的方式是用表达式 ? 真时执行 : 假时执行
,它有时会被串联,例如
function escape_string(io::IO, s::AbstractString, esc="")
a = Iterators.Stateful(s)
for c::AbstractChar in a
if c in esc
print(io, '\\', c)
elseif isascii(c)
c == '\0' ? print(io, escape_nul(peek(a)::Union{AbstractChar,Nothing})) :
c == '\e' ? print(io, "\\e") :
c == '\\' ? print(io, "\\\\") :
'\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) :
isprint(c) ? print(io, c) :
print(io, "\\x", string(UInt32(c), base = 16, pad = 2))
elseif !isoverlong(c) && !ismalformed(c)
isprint(c) ? print(io, c) :
c <= '\x7f' ? print(io, "\\x", string(UInt32(c), base = 16, pad = 2)) :
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 4 : 2)) :
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 8 : 4))
else # malformed or overlong
u = bswap(reinterpret(UInt32, c)::UInt32)
while true
print(io, "\\x", string(u % UInt8, base = 16, pad = 2))
(u >>= 8) == 0 && break
end
end
end
end
julia本身不提供switch case
结构,因为会自动优化