Conditional Statements in Julia
Now that we’ve covered looping, we can repeat whatever we do. Thus, today we cover conditionals, so as to be able to choose when we do it.
We all know this, so let’s jump into code:
julia> x=1;y=2;z=3;if x<y println("first") elseif y<z println("second") else println("default") end
Just a reminder, the indentation is immaterial. And again, as per usual,
else blocks are optional.
We can write an if loop in one-line as:
julia> x==y ? println("ok") : println("no")
Alas, that’s too tedious, because its Julia we’re in, we can do a more succinct version:
julia> println(x==y ? "equal" : "unequal")
And of course you can chain ternary operators.
julia> println(x<y ? "x" : y<z ? "y" : "z")#Finding the least valued variable
And that’s it for today! Next up, we discuss a truly distinguishing feature of Julia, the way it deals with arrays.