**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses 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

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

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

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

not surprisingly also
important in spreadsheets.
0:12

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

in the function.
0:21

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

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

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

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

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

then walk through a couple of examples.
0:45

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

Hit Enter, write the equals sign.
0:51

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

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

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

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

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

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

But note the syntax.
1:19

If a logical expression, comma,
1:21

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

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

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

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

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

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

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

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

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

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

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

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

Let's look at another example.
2:29

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

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

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

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

5, back to try again.
2:49

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

You get an error.
3:00

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

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

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

they have to be in quotation marks.
3:08

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

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

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

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

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

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

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

So here A6 is greater than 3,
3:57

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

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

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

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

that is not greater than three,
we've got the result too small.
4:22

You need to sign up for Treehouse in order to download course files.

Sign up