The IF function is one of the most popular functions in Excel, and it allows you to make logical comparisons between a value and what you expect. In its simplest form, the IF function says:

IF(Something is True, then do something, otherwise do something else)
So an IF statement can have two results. The first result is if your comparison is True, the second if your comparison is False.
Technical details
Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false.
Syntax
IF(logical_test, value_if_true, [value_if_false])
For example:

=IF(A2>B2,"Over Budget","OK")

=IF(A2=B2,B4A4,"")
Argument name 
Description 
logical_test (required) 
The condition you want to test. 
value_if_true (required) 
The value that you want returned if the result of logical_test is TRUE. 
value_if_false (optional) 
The value that you want returned if the result of logical_test is FALSE. 
Simple IF examples

=IF(C2=”Yes”,1,2)
In the above example, cell D2 says: IF(C2 = Yes, then return a 1, otherwise return a 2)

=IF(C2=1,”Yes”,”No”)
In this example, the formula in cell D2 says: IF(C2 = 1, then return Yes, otherwise return No)
As you see, the IF function can be used to evaluate both text and values. It can also be used to evaluate errors. You are not limited to only checking if one thing is equal to another and returning a single result, you can also use mathematical operators and perform additional calculations depending on your criteria. You can also nest multiple IF functions together in order to perform multiple comparisons.
Note: If you are going to use text in formulas, you need to wrap the text in quotes (e.g. “Text”). The only exception to that is using TRUE or FALSE, which Excel automatically understands.
How to get started
The best way to start writing an IF statement is to think about what you are trying to accomplish. What comparison are you trying to make? Many times, writing an IF statement can be as simple as thinking through the logic in your head: “what should happen if this condition is met vs. what should happen if it’s not?” You will always want to make sure that your steps follow a logical progression, or else your formula won’t do what you think it should. This is especially important when you create complex (nested) IF statements.
More IF examples

=IF(C2>B2,”Over Budget”,”Within Budget”)
In the above example, the IF function in D2 is saying IF(C2 Is Greater Than B2, then return “Over Budget”, otherwise return “Within Budget”)

=IF(C2>B2,C2B2,0)
In the above illustration, instead of returning a text result, we are going to return a mathematical calculation. So the formula in E2 is saying IF(Actual is Greater than Budgeted, then Subtract the Budgeted amount from the Actual amount, otherwise return nothing).

=IF(E7=”Yes”,F5*0.0825,0)
In this example, the formula in F7 is saying IF(E7 = “Yes”, then calculate the Total Amount in F5 * 8.25%, otherwise no Sales Tax is due so return 0)
Best Practices  Constants
In the last example, you see both “Yes”, and the Sales Tax Rate (0.0825) entered directly into the formula. It is generally not a good practice to put literal constants (values that may need to change from time to time) directly into formulas, because they can be hard to find and change in the future. It’s much better to put constants into their own cells, where they are out in the open and easily found and changed. In this case, it’s OK, because there’s only one IF function and the Sales Tax Rate will rarely change. Even if it does, it will be easy to change it in the formula.
Using IF to check if a cell is blank
Sometimes you need to check if a cell is blank, generally because you might not want a formula to display a result without input.
In this case we're using IF with the ISBLANK function:

=IF(ISBLANK(D2),"Blank","Not Blank")
Which says IF(D2 is blank, then return "Blank", otherwise return "Not Blank"). You could just as easily use your own formula for the "Not Blank" condition as well. In the next example we're using "" instead of ISBLANK. The "" essentially means "nothing".

=IF(D3="","Blank","Not Blank")
This formula says IF(D3 is nothing, then return "Blank", otherwise "Not Blank"). Here is an example of a very common method of using "" to prevent a formula from calculating if a dependent cell is blank:

=IF(D3="","",YourFormula())
IF(D3 is nothing, then return nothing, otherwise calculate your formula).
Nested IF example
Where a simple IF function has only two results (True or False), nested IF functions can have from 3 to 64 results.

=IF(D2=1,”YES”,IF(D2=2,”No”,”Maybe”))
In the above illustration, the formula in E2 says: IF(D2 equals 1 then return “Yes”, otherwise IF(D2 equals 2 then return “No”, otherwise return “Maybe”).
A word of caution
While Excel will allow you to nest up to 64 different IF functions, it’s not at all advisable to do so. Why?

Multiple IF statements require a great deal of thought to build them correctly and make sure that their logic can calculate correctly through each condition all the way to the end. If you don’t nest your IF statements 100% accurately, then a formula might work 75% of the time, but return unexpected results 25% of the time. Unfortunately, the odds of you catching the 25% is slim.

Multiple IF statements can become very difficult to maintain, especially when you come back some time later and try to figure out what you, or worse someone else, was trying to do.
Common problems
Problem 
What went wrong 
0 (zero) in cell 
There was no argument for either value_if_true or value_if_False arguments. To see the right value returned, add argument text to the two arguments, or add TRUE or FALSE to the argument. 
#NAME? in cell 
This usually means that the formula is misspelled. 
More resources

Read an Overview of formulas. You'll learn all about the bits and pieces that make them up.

Learn how to Use nested functions in a formula.

Excel has additional functions that can be used to analyze your data based on a condition. For example, to count the number of occurrences of a string of text or a number within a range of cells, use the COUNTIF or the COUNTIFS worksheet functions.

To calculate a sum based on a string of text or a number within a range, use the SUMIF or the SUMIFS worksheet functions.

See a video on how to use the IF function.

Do you have suggestions about how we can improve the next version of Excel? If so, please check out the topics at Excel User Voice.