딥스탯 2018. 9. 18. 20:59
5_Conditionals

Conditionals

Reference

https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia (github : JuliaComputing/JuliaBoxTutorials/introductory-tutorials/intro-to-julia/)

Topics:

  1. With the if keyword
  2. With ternary operators
  3. With short-circuit evaluation
  4. Exercises

Series

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:

In [1]:
N = 3
Out[1]:
3
In [2]:
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
Fizz
In [3]:
N = 5
Out[3]:
5
In [4]:
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
Buzz
In [5]:
N = 15
Out[5]:
15
In [6]:
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
FizzBuzz
In [7]:
N = 1
Out[7]:
1
In [8]:
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
1

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:

In [9]:
x = 1
y = 2
Out[9]:
2

Using the if and else keywords, we might write:

In [10]:
if x > y
    x
else
    y
end
Out[10]:
2

and as a ternary operator, the conditional looks like this:

In [11]:
(x > y) ? x : y
Out[11]:
2
In [12]:
x = 2
y = 1
Out[12]:
1
In [13]:
if x > y
    x
else
    y
end
Out[13]:
2
In [14]:
(x > y) ? x : y
Out[14]:
2

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:

In [15]:
false && (println("hi"); true)
Out[15]:
false
In [16]:
true && (println("hi"); true)
hi
Out[16]:
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:

In [17]:
x = 1
(x > 0) && error("x cannot be greater than 0")
x cannot be greater than 0

Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at In[17]:2
In [18]:
x = -1
(x > 0) && error("x cannot be greater than 0")
Out[18]:
false

Similarly, check out the || operator, which also uses short-circuit evaluation to perform the "or" operation.

In [19]:
true || println("hi")
Out[19]:
true
In [20]:
false || println("hi")
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.

In [21]:
n = 1
Out[21]:
1
In [22]:
if n % 2 == 1
    println("odd")
else
    println(n)
end
odd
In [23]:
n = 2
Out[23]:
2
In [24]:
if n % 2 == 1
    println("odd")
else
    println(n)
end
2

5.2

Rewrite the code from 5.1 using a ternary operator.

In [25]:
n = 1
Out[25]:
1
In [26]:
(n % 2 == 1) ? println("odd") : n
odd
In [27]:
n = 2
Out[27]:
2
In [28]:
(n % 2 == 1) ? println("odd") : n
Out[28]:
2