Let’s ask a simple question. What can we currently do if we want our code to handle different cases?
Let’s ask a simple question. What can we currently do if we want our code to handle different cases?
For example, suppose we would like to get the season (as a string) given a month.
We could use a vector.
This is a neat work around, but in more complex cases this won’t be possible (or practical).
Let’s consider another case: the famous FizzBuzz problem.
Here’s an idea:
Oh my, can you make sense of this?
Maybe this?
Umm… Are your eyes hurting yet?
What about this?
… Isn’t that terrible? Can you explain how it works?
Clearly we need some better tools for handling cases.
if
Only!Recall that we prefer for our code to be expressive.
Our description of fizzbuzz
included “IF”. Wouldn’t it be nice to write this directly in our code?
if
StatementThe if
statement follows the intuitive structure:
if (<logical>) <body>
where <body>
is executed if <logical>
is true.
Note that the parenthesis are required1.
if
statement – multiline body?What if we want to execute multiple lines of code if a condition is true?
if
statement – multiline body?We rewrite do_some_math
:
Note that when using curly brackets, the last expression evaluated will be the value of the curly bracket block.
What is lover
?
Maybe we’d like to execute some code if a condition is true, and execute some other code otherwise. We could write,
But this:
conditional
twice. If the first if
failed, we know conditional
is FALSE
.body A
modifies conditional
.if else
StatementInstead, we use the if else
statement:
if (<logical>) <body A> else <body B>
where,
<body A>
is executed if <logical>
is true.<body B>
is executed if <logical>
is false.else if
StatementWhen we need to account for more cases than just two (true or false), we can use an else if
statement:
if (<logical 1>)
<body 1>
else if (<logical 2>)
<body 2>
else if (<logical 3>)
<body 3> ...
else
<body N>
if
statement is required, but the final else
is not.else if
s as you’d like.The curly brackets are optional if the body is one line.
Note that R requires else/else if
be placed after the closing curly bracket (}
).
This is not allowed.
You may see these statement referred to as,
which_season
Let’s rewrite our which_season
function:
which_season
Now with control flow statements.
fizzbuzz
Let’s rewrite our fizzbuzz
function:
fizzbuzz
Now with control flow statements. Pick your poison!
if else
, ifelse()
We can use the function ifelse
to apply the if else
operation to a vector. Its function signature is
where,
test
is a vector of logical values.yes
is the value to be placed in the result vector if the corresponding logical value is true.no
is the value to be placed in the result vector if the corresponding logical values is false.ifelse()
Example [1] "Not divisible by 2" "Divisible by 2" "Not divisible by 2"
[4] "Divisible by 2" "Not divisible by 2" "Divisible by 2"
[7] "Not divisible by 2" "Divisible by 2" "Not divisible by 2"
[10] "Divisible by 2"