Getting conditional with IF-THEN-ELSE

Video Contents

0:00 Do you need a SKIPIF instead?

0:10 When to use IF-THEN-ELSE

0:26 How to read an IF-THEN-ELSE

1:12 What you'll learn

1:36 A single criterion IF-THEN-ELSE fields

3:06 How to evaluate multiple criteria like with AND

3:32 Nesting IF-THEN-ELSE fields to evaluate multiple criteria

4:40 Using a formula in an IF-THEN-ELSE to evaluate multiple criteria

6:56 Why a formula might be better than nesting

7:16 How to evaluate multiple criteria like with OR

8:24 I'll see you in the next video!

Video Transcript

If you're talking about skipping over entire responses or records that meet certain criteria, you're talking about something called a skip if, and we'll cover that in a later lesson.

But, if you're talking about using values in your data to conditionally display fields, conditionally display entire sections of a document, or conditionally display some predetermined text, you're talking about an if-then-else situation. If some criteria is true, then do this, otherwise or else, do this other thing. In field code view, these types of conditionals follow a standard format: they'll be inside curly braces, and the first thing inside of the curly brace will be the field name if, followed by the expressions you're evaluating, your criteria, followed by something called the true text, which is what you want to do if the expressions evaluate to true, so this is the then part of the if-then-else, followed by something called the false text, which is what you want to do if the expressions evaluate to false, so this is the else part of the if-then-else. To give a feel for how the if-then-else works, and how it can be a powerful tool, I'm going to work through some simple conditionals that don't rely on any data, but that build up to show you that true text and false text don't always have to be text, and what to do if you have multiple expressions or criteria that you want to check, that is, how to deal with logical ands and logical ors.

So, in field code view, our first if-then-else will be if 5 is equal to 5, which it is, display the word true, otherwise display the word false. If I preview results, you'll see that we get the word true as expected. Now, if I change my expression to be 6 equals 5, in preview results we get the word false as expected. What if when our expressions evaluate to false we want to display nothing? I'll set the false text to what's called empty string, that is, double quotation marks with nothing inside of them, not even a space. I'll put a label in front of this one so you can see where it's supposed to go. If I preview results, you can see there's nothing being displayed after the label. Now, those are all text examples.

What about returning numbers? In the world of computer science, trues are usually represented by the number one and falses as zeros, so I'll change my true text and false text to the digits one and zero accordingly. So, if six is equal to five, display a one, otherwise display a zero. And if I preview results, you can see we get the number zero.

In each of these cases so far, our evaluations have been pretty simple, but real world evaluations are rarely this easy.

What if we want to see if two things are true at the same time, like if 5 equals 5 and 6 equals 6? Word doesn't give us an easy way to just type that in with the word and, but we can use the if-then-else syntax in two different ways to pull this off: as a nested if-then-else and as a calculated formula as our expression to evaluate. I'll start with the nested if-then-else. Nesting means that we're going to put an if-then-else inside of another if-then-else. And we're going to use a new if statement for each of our criteria. So our first if-then-else will check if 5 equals 5 with true text and false text values, and our second if-then-else will check if 6 equals 6 with true text and false text set to the same values as the 5 equals 5 check. To check if both are true at the same time, we'll nest the check for 6 equals 6 inside the check for 5 equals 5 by moving the entire if-then-else for the 6 equals 6 into the true text spot of the check for 5 equals 5. And now you can see that we'll first check if 5 equals 5, and, if it is, then we'll check if 6 equals 6, and, if that's true, too, then we get the true text. And in all other cases we get the false text. So that's nesting if-then-elses. The second way to check if multiple criteria are true at the same time is to use calculations. For this method, we'll start with checks that return zeros and ones in the computer science way, and we're going to put them inside of a formula field as our expression to evaluate so that we can sort of tally how true our criteria are. Formulas also go inside of curly braces, where the first thing inside of the curly brace is an equal sign, followed by some math. In our case, our formula needs to add the text returned by the check for five equals five and the text returned by the check for six equals six. So, in the new pair of curly braces, I'll start with an equal sign, followed by the five equals five check, followed by a plus sign, followed by the six equals six check. If I preview results, so far you can see that individually the checks each return a one, and added together in the formula they return a two. So, to check if they're both true at the same time, we can put this formula inside of an if statement that has the true text and false text for what to do when they're both true, that is, when adding the checks together is equal to two, and what to do when it's not two, that is, when one or both of the criteria are false. Basically, though, you're checking to see that your formula is equal to the number of checks you're making here, so that number two changes to reflect your total number of criteria. So with three checks, it would need to be three, and so on. And now if I preview results, you can see we get the true text. If I make a copy of this and change the six equals six test to read 6 equals 7, which is false, you can see that the calculation would now evaluate to 1, which is 1 plus 0, which is not equal to 2, and so the if-then-else returns the false text. So that's how to use a formula calculation to check if multiple criteria are true at the same time. When I'm dealing with multiple criteria, I prefer this method over nesting, because I find it easier to update if I change what I want to have happen when the evaluation is false - I only have to change it once instead of every time the false text appears in all of the nested checks.

Now, what if we wanted to check if any of the criteria are true to get the true text, that is, if 5 equals 5 or 6 equals 6 or 6 equals 7? We can use a variation of the calculation method we just used for and. All we need to do is change how we evaluate the formula. Instead of checking to see if the formula evaluates to the number of criteria we're using, we just need to check to see if it's at least 1, that is, greater than or equal to 1, because we'll be happy if 5 equals 5 or 6 equals 6 or 6 equals 7. In this case, our formula will evaluate to two, because two of our criteria are true. And our evaluation is flexible enough to handle this result and all of the other possibilities. We'll get the true text with one true criteria, two true criteria, and with three true criteria. In fact, it only fails when all of the criteria evaluate to false, which is how an or works.

I'll see you in the next video!

Complete and Continue