Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The IF function is called a conditional function. It returns a value based on logic you specify. For example, say you wanted to know how many years certain countries had populations less than 10 million people? Or how many days a stock price was higher than $100? Or how many pieces of wood had a width of exactly 7 inches? You can answer all these questions, and many more using the IF function.
Example Files
 you can continue to use the spreadsheet from the last video, or
 open a copy of this spreadsheet to catch up to this video
Reference

0:00
Now that you have a solid understanding of sum, average and median.

0:05
Lets introduce a new function, the if function.

0:09
If is a pretty foundational concept in computer programming and

0:12
not surprisingly also important in spreadsheets.

0:16
If is a conditional function that returns a value based on logic you put

0:21
in the function.

0:22
For example, say you wanted to know how many years certain countries had

0:27
populations less than 10 million people or how many days a stock price was higher

0:32
than a $100 ,or how many pieces of wood had a width of exactly three inches?

0:38
You can answer all of these questions and many more using the if function.

0:43
Let's take a look at the function syntax and

0:45
then walk through a couple of examples.

0:48
>> So again, we select a cell.

0:51
Hit Enter, write the equals sign.

0:54
Then IF, then parenthesis and we can see the assistance below.

1:02
Logical expression can be many things, for example

1:05
you can test if the values in two cells are the same using the equality operator.

1:10
Or you can test if two values are not the same.

1:12
You'd use an inequality operator and so forth.

1:15
The example that Google Sheets has given is text based.

1:19
But note the syntax.

1:21
If a logical expression, comma,

1:24
then the value that is returned if it's true, the logical expression is true.

1:29
Then a comma then a value that's returned if the logical expression is false.

1:34
So let's walk through an example of this together.

1:39
In this cell, we've written a formula based on if that says,

1:46
if cell A9 is equal to the text Happy,

1:52
then if true, return the text that says A9 is Happy, and

1:57
comma, then if it's not true, return the text that says A9 is not Happy.

2:03
So we can see here cell A9 is not Happy, so this is returning A9 is not happy.

2:13
What happens if I actually write Happy in cell A9.

2:16
You can see that this automatically changed to read A9 is happy.

2:22
So I can undo that, A9 is not happy.

2:25
I can redo it, A9 is happy.

2:29
Let's look at another example.

2:31
If E8 is equal to 4, then return 1.

2:38
If it's not return a text, try again.

2:43
So it's return and try again.

2:45
If I make this 4, now it's 1.

2:49
5, back to try again.

2:53
Notice for text, that you have to put it in quotation marks, or it won't work.

3:00
You get an error.

3:01
And the description of the error is formula parse error.

3:03
For a beginner, that's not very helpful.

3:05
But just note that when you're writing text and formulas,

3:08
they have to be in quotation marks.

3:17
We can use this if function in another way as well, so

3:21
let's just show you one more example.

3:24
Where instead of an equality or an inequality we have a greater than.

3:28
So what I'm gonna do is I'm gonna write here, if.

3:33
And I wanna display all the pieces of wood that have heights greater than three.

3:41
So if A6 is greater than 3, then I'm going to return true.

3:50
If it's not, Too small, for example.

3:57
So here A6 is greater than 3,

3:59
in this case it's actually 6, so it returns a value True.

4:05
Now I'm gonna copy this formula, All the way down.

4:12
And we can see that through relative referencing,

4:17
we've been able to carry this formula all the way down and for every piece of wood

4:22
that is not greater than three, we've got the result too small.
You need to sign up for Treehouse in order to download course files.
Sign up