Like any other expression, the return value of an if
...else
expression can be ignored (and hence discarded). This is generally only useful when the body of the expression has side effects, such as writing to a file, mutating variables, or printing to the screen.
Furthermore, the else
branch of an if
...else
expression is optional. For instance, we can write the following code to output to screen only if a particular condition is met:
second = Dates.second(now())
if iseven(second)
println("The current second, $second, is even.")
end
In the example above, we use time and date functions to get the current second; for instance, if it is currently 10:55:27, the variable second
will hold 27
. If this number is even, then a line will be printed to screen. Otherwise, nothing will be done.