05. Conditionals
Conditionals¶
Reference¶
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)
Topics:
Series¶
- http://deepstat.tistory.com/45 (01. Getting started)(in English)
- http://deepstat.tistory.com/46 (01. Getting started(한글))
- http://deepstat.tistory.com/47 (02. Strings)(in English)
- http://deepstat.tistory.com/48 (02. Strings(한글))
- http://deepstat.tistory.com/49 (03. Data structures)(in English)
- http://deepstat.tistory.com/50 (03. Data structures(한글))
- http://deepstat.tistory.com/51 (04. Loops)(in English)
- http://deepstat.tistory.com/52 (04. Loops(한글))
- http://deepstat.tistory.com/54 (05. Conditionals(한글))
With the if keyword ¶
In Julia, the syntax
if *condition 1*
*option 1*
elseif *condition 2*
*option 2*
else
*option 3*
end
allows us to conditionally evaluate one of our options.
For example, we might want to implement the FizzBuzz test: given a number, N, print "Fizz" if N is divisible by 3, "Buzz" if N is divisible by 5, and "FizzBuzz" if N is divisible by 3 and 5. Otherwise just print the number itself! Enter your choice for N here:
N = 3
if (N % 3 == 0) && (N % 5 == 0) # `&&` means "AND"; % computes the remainder after division
println("FizzBuzz")
elseif N % 3 == 0
println("Fizz")
elseif N % 5 == 0
println("Buzz")
else
println(N)
end
N = 5
if (N % 3 == 0) && (N % 5 == 0) # `&&` means "AND"; % computes the remainder after division
println("FizzBuzz")
elseif N % 3 == 0
println("Fizz")
elseif N % 5 == 0
println("Buzz")
else
println(N)
end
N = 15
if (N % 3 == 0) && (N % 5 == 0) # `&&` means "AND"; % computes the remainder after division
println("FizzBuzz")
elseif N % 3 == 0
println("Fizz")
elseif N % 5 == 0
println("Buzz")
else
println(N)
end
N = 1
if (N % 3 == 0) && (N % 5 == 0) # `&&` means "AND"; % computes the remainder after division
println("FizzBuzz")
elseif N % 3 == 0
println("Fizz")
elseif N % 5 == 0
println("Buzz")
else
println(N)
end
With ternary operators ¶
For this last block, we could instead use the ternary operator with the syntax
a ? b : c
which equates to
if a
b
else
c
end
Now let's say we want to return the larger of two numbers. Give x and y values here:
x = 1
y = 2
Using the if and else keywords, we might write:
if x > y
x
else
y
end
and as a ternary operator, the conditional looks like this:
(x > y) ? x : y
x = 2
y = 1
if x > y
x
else
y
end
(x > y) ? x : y
With short-circuit evaluation ¶
We've already seen expressions with the syntax
a && b
to return true if both a and b are true. Of course, if a is false, Julia doesn't even need to know the value of b in order to determine that the overall result will be false. So Julia doesn't even need to check what b is; it can just "short-circuit" and immediately return false. The second argument b might be a more complicated expression like a function call with a side-effect, in which case it won't even be called:
false && (println("hi"); true)
true && (println("hi"); true)
On the other hand, if a is true, Julia knows it can just return the value of b as the overall expression. This means that b doesn't necessarily need evaluate to true or false! b could even be an error:
x = 1
(x > 0) && error("x cannot be greater than 0")
x = -1
(x > 0) && error("x cannot be greater than 0")
Similarly, check out the || operator, which also uses short-circuit evaluation to perform the "or" operation.
true || println("hi")
false || println("hi")
Exercises ¶
5.1¶
Write a conditional statement that prints a number if the number is even and the string "odd" if the number is odd.
n = 1
if n % 2 == 1
println("odd")
else
println(n)
end
n = 2
if n % 2 == 1
println("odd")
else
println(n)
end
5.2¶
Rewrite the code from 5.1 using a ternary operator.
n = 1
(n % 2 == 1) ? println("odd") : n
n = 2
(n % 2 == 1) ? println("odd") : n