Logical Functions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Logical Functions

Introduction

Because Microsoft Excel is not a programming environment, it doesn't use conditional statements seen in traditional languages such as C/C++, Pascal, C#, Visual Basic, etc. Instead, Microsoft Excel provides functions that can perform the same types of tests on cells' values and produce the same types of results

A logical function is one that evaluates an expression and returns a Boolean result. For example, imagine you have a series of cells that represent employees last name. If you are interested to know what cell doesn't have a value (a last name), you can use a function. On the other hand, imagine you have a cell that is supposed to indicate whether an item must receive a discount, you can use a conditional function to check it.

Most, if not all, logical functions check a condition and render a result. The condition is also called a criterion. A criterion can be something like "Check if the cell contains Male", or "Check if the salary is less than $12.35".

IF

To check whether a criterion is true or false before taking a subsequent action, you can use the IF() function. Its syntax is:

IF(ConditionToTest, WhatToDoIfConditionIsTrue, WhatToDoIfConditionIsFalse)

When it is called, the IF() function checks the truthfulness or negativity of the ConditionToTestargument. If the result is true, then it will execute the first expression, WhatToDoIfConditionIsTrue in our syntax. If the result is false, it will consider the second option, the WhatToDoIfConditionIsFalse parameter in our syntax.

Nested IF

The IF() function we have introduced above is used to check one condition and then take one or the other action. In some cases, you will need to check more than one condition. In other words, you may want to check a first condition. If that condition is false, you may want to yet check another condition. The pseudo-code used for such a scenario is:

If Condition1 is True
 
Execute Statement1
Else If Condition2 is True
 
Execute Statement2
Else
 
Execute ElseStatement

Consequently, you can check as many conditions as you need to. The truthfulness of each condition would lead to its own statement. If none of the conditions is true, then you can execute the last statement. To implement this scenario, you can include an IF() function inside of another. You can also include as many IF() functions inside of other IF() functions.

We are going to use the IF() function to find out whether a student passes the mark or fails the class, based on the overall average of the student's grades. We will set the mark at 12.50. If a student's average is above 12.50, then he goes to the next grade, otherwise, …

 Practical Learning: Using the IF Functions

  1. Open the Red Oak High School workbook from Lesson 11
  2. In cell B13, type Pass/Fail?
  3. In cell C13, type =IF(
  4. Click cell C12 and type >=12.50,"Pass","Fail")
  5. Press Enter
  6. Click cell C13 to give it focus
  7. Drag its Fill Handle to cell H15. 
    You can also put an IF function inside of another, this is referred to as nesting
  8. In cell C14, type =IF(B14>16.5,"A",IF(B14>14.5,"B",IF(B14>12.5,"C",IF(B14>12,"D","F")))) and press Enter

Checking For Cell Emptiness

To check whether a cell is empty, you can call the Boolean ISBLANK() function.

Conditional Counting

In some cases you will need to count the frequency of (the number of times that) a value repeat itself in a series of cells. For example, suppose you have a list of students recognized by their gender using a certain column. Provided the genders are, for example, Male or Female. You may be interested in knowing the number of boys. In this case you can count the number of occurrences of Male in the range of cells.

The count the number of occurrences of a value in a series, you can use the COUNTIF()function. Its syntax is

COUNTIF(Range, Criteria)

The functions include MDETERM, MINVERSE, MMULT, PRODUCT, SUMSQ, SUMX2MY2, SUMX2PY2, and SUMXMY2.