7  Conditional

Julia doc: Conditional Operation

7.1 IF

if *condition 1*
    *option 1*
elseif *condition 2*
    *option 2*
else
    *option 3*
end
function FizzBuzz(๐Ÿฅš)
    
    if (๐Ÿฅš % 3 == 0) && (๐Ÿฅš % 5 == 0)
        println("Fizz Buzz ๐ŸšŒ")
    elseif ๐Ÿฅš % 3 == 0
        println("$(๐Ÿฅš รท 3) Fizz")
    elseif ๐Ÿฅš % 5 == 0
        println("$(๐Ÿฅš รท 5) Buzz ๐ŸšŒ")
    else
        println("$๐Ÿฅš")
    end

end
FizzBuzz (generic function with 1 method)
FizzBuzz(15)
Fizz Buzz ๐ŸšŒ

if return a value, so we can assign a value after that.

x = 2

โ“ = if x > 0
        "positive"
    else
        "negative"
    end
        
โ“
"positive"

Boolean in if must be true or false.

if 1
    println("true")
end
# This will error

7.2 Ternary operators

a ? b : c

Translate to

if a
    b
else
    c
end
what_is_larger(x, y) = x > y ? "$x is larger than $y" : "$y is larger than $x" 

what_is_larger(1, 2)
"2 is larger than 1"