Conditional Logic
For our purposes, conditional logic is two words - "IF" and "Then". You use conditional logic all the time in your daily life, without realising it. You might say to yourself, "IF I eat this delicious cream cake THEN my diet will be ruined." You're using conditional logic to make a decision: IF I do this THEN that will happen. Some more examples:
IF I buy this lovely coat THEN I will look beautiful
IF I watch one programme THEN I can tape the other
IF I win the lottery THEN I will be happy
Those are all example of conditional logic. Excel also uses the IF word for conditional logic. You can test what is in a cell, and say what should happen if it is one value rather than another. For example, suppose cell A1 has the number 6 in it. In cell A2, you can enter an IF function to test whether cell A1 is above 5 or below 5. IF it is above 5 THEN one thing happens; IF it is below 5 THEN another thing happens.
The correct format to use for the IF function is this:
IF(logical_test, value_if_false, value_if_true)
So the IF function takes three arguments: logical test, value if true, value if false. Let's break those three arguments down a little more:
- Logical Test:
- This is what you want to test for. In our example, we wanted to test whether cell A1 is greater than or less than 5. Excel uses this symbol > for greater than and this symbol < for less than. So for the first argument, we would put A1 > 5
- Value If True:
- This is the THEN part of the IF statement. Excel needs to know what you want to happen IF your condition is met. You can put in text surrounded by quotes, or another formula.
- Value If False:
- You also need to say what should happen IF your condition is not met.
Let's clarify all this with a spreadsheet example. So, start a new spreadsheet and do the following:
- Click inside cell A1 and enter the number 6
- Press the return key on your keyboard
- Widen the B column of your spreadsheet. Make it nice and big.
- Click inside cell B1
- Click inside the formula bar
- Enter the following formula:
=IF(A1 > 5, "Greater than Five", "Less than Five")
- Press the return key on your keyboard
- Your spreadsheet should look like this one below:
It's important you get all the syntax right in your IF function. In other words, don't forget the commas, and the double quote marks around text.
But click inside cell A1 and change the number 6 to the number 4. Then press the return key on your keyboard. Excel should put Less than Five in cell B1. Now put the number 5 in cell A1 and see what happens.
Nothing happened, right? That's because the IF function is very precise. You didn't tell Excel what to do if the number equalled 5.
To get round this, we can use the Greater Than ( > ) symbol and the Equal symbol together. Like this:
A1 >= 5
That says "A1 greater than or equal to 5". When we amend our function, it looks like this:
=IF(A1 >= 5, "Greater than or Equal to Five", "Less than Five")
You can also test to see if something is Less Than ( < ) or Equal to. You could use this for the formula:
=IF(A1 <= 5, "Less than or Equal to Five", "Greater than Five")
You can even test for Not Equal To. Just join the Less Than (<) symbol to the Greater Than (<) symbol. Like this:
=IF( A1 <> 5, "Not 5", "Is 5")
So to sum up: after typing IF you tell Excel what you want to test for. Then you say what should happen if the condition is true. Then you say what should happen if the condition is false.
No comments:
Post a Comment