zoukankan      html  css  js  c++  java
  • Julia

    Julia 中使用 if,elseif,else 进行条件判断

    格式:

    if expression1
    	statement1
    elseif expression2
    	statement2
    else
    	statement3
    end
    

    如果条件 expression1 成立,则执行语句 statement1

    如果条件 expression2 成立,则执行语句 statement2

    如果条件 expression1 和条件 expression2 都不成立,则执行语句 statement3

    elseif 和 else 是可选的

    elseif 可以有多个,但是 else 只能有一个

    julia> function f(x, y)
               if x < y
                   println("x is smaller than y")
               elseif x > y
                   println("x is bigger than y")
               else
                   println("x is equal to y")
               end
           end
    f (generic function with 1 method)
    
    julia> f(1, 2)
    x is smaller than y
    
    julia> f(2, 1)
    x is bigger than y
    
    julia> f(1, 1)
    x is equal to y
    

    条件表达式的值只能是 true 和 false,不能用 0 和 1 代替

    julia> if true
               println("True")
           end
    True
    
    julia> if 1
               println("True")
           end
    ERROR: TypeError: non-boolean (Int64) used in boolean context
    Stacktrace:
     [1] top-level scope at none:0
    
    julia> if false
               println("False")
           end
    
    julia> if 0
               println("False")
           end
    ERROR: TypeError: non-boolean (Int64) used in boolean context
    Stacktrace:
     [1] top-level scope at none:0
    
  • 相关阅读:
    使用kindeditor时,取不到值
    .net Eval 绑定截取字符串
    学习GeoServer遇到的问题及答案
    爬虫用开源代理池比较
    jenkins自动化部署gitlab上maven程序
    Notepad++编辑.sh文件
    springboot的jar在linux运行
    python3安装web.py
    linux java -jar
    linux安装nginx
  • 原文地址:https://www.cnblogs.com/sch01ar/p/9526836.html
Copyright © 2011-2022 走看看