# Control Flow

• TODO: intro

After reading these notes you should be able to:

• TODO: learning objectives

## `if`

Spring 2022 Readers: Dave tried to write too many notes this week. Please read the following in place of this under construction section:

``if (condition) code_to_evaluate``
``if (TRUE) print("Hello, World!")``
``[1] "Hello, World!"``
``````if (FALSE) print("Hello, World!")
if (42) print("Hello, World!")``````
``[1] "Hello, World!"``
``if ("foo") print("Hello, World!")``

## `else`

Spring 2022 Readers: Dave tried to write too many notes this week. Please read the following in place of this under construction section:

``````x = 42
if (x > 50) {
y = 1
} else {
y = 0
}
y``````
``[1] 0``
• TODO: do something “else” if condition not met
• TODO: can be chained with additional ifs

## `switch`

• TODO: we won’t do this in a quiz or exam, so not currently needed

## `ifelse`

Spring 2022 Readers: Dave tried to write too many notes this week. Please read the following in place of this under construction section:

• TODO: “vectorized” if-else
``ifelse(test = 42 > 2, yes = "!", no = "?")``
``[1] "!"``
``ifelse(test = 2 > 42, yes = "!", no = "?")``
``[1] "?"``
• TODO: full vector example
• TODO: vector example with recycling
``ifelse(1:10 %% 2 == 0, "even", "odd")``
`` [1] "odd"  "even" "odd"  "even" "odd"  "even" "odd"  "even" "odd"  "even"``

## Loops

Loops (`for`, `while`, and `repeat`) are another form of control flow. We will put off discussing these until next chapter so that we can first introduce the apply functions as a more common R approach to performing the types of operations usually done in other languages with loops.

## Summary

• TODO: You’ve learned to…

## What’s Next?

• TODO: iteration

• TODO: ?