1. Flow control
Flow control is a computer science term for running different code under different conditions. The basic principle of flow control is "if some condition happens, do something, otherwise do something else".
2. If this then that
The IF() function takes three arguments: a logical value for the condition, the value to return if the condition is TRUE, and the value to return if it is FALSE.
Typically, the condition isn't just going to be the actual value TRUE or FALSE, it will be the result of a calculation. In the example, you can see that A1 is five, so the condition "is A2 greater than zero" is met, and the first option is returned.
3. Dealing with lots of conditions
If you have several conditions, you could just start nesting IF() functions. That code can get messy though, so fortunately there's a better way. The IFS() function, with an S on the end, accepts an arbitrary number of conditions to test, and returns the value corresponding to the first condition that is met.
In the first and third rows of the example, the first condition is met, so the first text value is returned. In the second row, the first condition fails, but the second one is met so the second value is returned.
One thing to note is that if no condition is met, it will return the missing value, N-dot-A-dot. You'll learn more about N-dot-A-dot in the next video.
4. Transforming categorical variables
The SWITCH() function is related to IFS(). Rather than matching logical conditions, it matches text values. In the previous slide you saw how IFS() was used to change logical conditions into categorical values, SWITCH() is really useful for transforming categorical variables.
In the first row of the example, you can see that the text value "first" is matched, so the value 1 is returned. Again, if there is no match, a missing value is returned.
5. Summary
This video covered three functions for flow control.
IF() returns a value based on a single logical condition. IFS() extends this to multiple conditions. Finally, SWITCH() transforms categorical variables.
6. Let's practice!
Now it's your turn to control the flow!