{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conditionals\n",
"\n",
"#### with the `if` keyword\n",
"In Julia, the syntax\n",
"\n",
"```julia\n",
"if *condition 1*\n",
" *option 1*\n",
"elseif *condition 2*\n",
" *option 2*\n",
"else\n",
" *option 3*\n",
"end\n",
"```\n",
"\n",
"allows us to conditionally evaluate one of our options.\n",
"
\n",
"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:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"N = 15"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FizzBuzz\n"
]
}
],
"source": [
"if (N % 3 == 0) && (N % 5 == 0) # `&&` means \"AND\"; % computes the remainder after division\n",
" println(\"FizzBuzz\")\n",
"elseif N % 3 == 0\n",
" println(\"Fizz\")\n",
"elseif N % 5 == 0\n",
" println(\"Buzz\")\n",
"else\n",
" println(N)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### with ternary operators\n",
"\n",
"For this last block, we could instead use the ternary operator with the syntax\n",
"\n",
"```julia\n",
"a ? b : c\n",
"```\n",
"\n",
"which equates to\n",
"\n",
"```julia\n",
"if a\n",
" b\n",
"else\n",
" c\n",
"end\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's say we want to return the larger of two numbers. Give `x` and `y` values here:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 2\n",
"y = 3.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the `if` and `else` keywords, we might write:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.5"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"if x > y\n",
" x\n",
"else\n",
" y\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and as a ternary operator, the conditional looks like this:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.5"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(x > y) ? x : y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### with short-circuit evaluation\n",
"\n",
"We've already seen expressions with the syntax\n",
"```julia\n",
"a && b\n",
"```\n",
"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:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"false"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"false && (println(\"hi\"); true)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hi\n"
]
},
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true && (println(\"hi\"); true)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"ename": "ErrorException",
"evalue": "x cannot be greater than 0",
"output_type": "error",
"traceback": [
"x cannot be greater than 0",
"",
"Stacktrace:",
" [1] error(::String) at .\\error.jl:33",
" [2] top-level scope at In[11]:1"
]
}
],
"source": [
"(x > 0) && error(\"x cannot be greater than 0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, check out the `||` operator, which also uses short-circuit evaluation to perform the \"or\" operation."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"true || println(\"hi\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hi\n"
]
}
],
"source": [
"false || println(\"hi\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises\n",
"\n",
"#### 5.1\n",
"Write a conditional statement that prints a number if the number is even and the string \"odd\" if the number is odd."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"odd\n",
"2\n",
"odd\n",
"4\n",
"odd\n",
"6\n",
"odd\n",
"8\n",
"odd\n",
"10\n",
"odd\n",
"12\n",
"odd\n",
"14\n",
"odd\n",
"16\n",
"odd\n",
"18\n",
"odd\n",
"20\n",
"odd\n",
"22\n",
"odd\n"
]
}
],
"source": [
"for i in 1:23\n",
" if (i % 2 == 0)\n",
" println(i)\n",
" else\n",
" println(\"odd\")\n",
" end\n",
" end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 5.2\n",
"Rewrite the code from 5.1 using a ternary operator."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"odd\n",
"2\n",
"odd\n",
"4\n",
"odd\n",
"6\n",
"odd\n",
"8\n",
"odd\n",
"10\n",
"odd\n",
"12\n",
"odd\n",
"14\n",
"odd\n",
"16\n",
"odd\n",
"18\n",
"odd\n",
"20\n",
"odd\n",
"22\n",
"odd\n"
]
}
],
"source": [
"for i in 1:23\n",
" (i % 2 == 0) ? println(i) : println(\"odd\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.4",
"language": "julia",
"name": "julia-1.3"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.4.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}