If-Then Statements

Topics

  • Syntax
  • Else
  • ElseIf
  • Cautions

Video


If-Then Statements

Syntax

If-Then statements are a type of variable logic that allows the output of the variable to be conditionally determined. For all If-Then statements, the conditions must be defined as well as the actions that should occur when those conditions are met.

If then statements have a standardized structure:

If condition_one (is_met) Then do_this

"Condition_one" and "do_this" are defined using our available objects, functions and operators.

Example: If condition_one Then do_this

=If [D Dept Code]="ART" Then "My Department"

This If-Then statement is saying that when the field Department Code is the three letters "ART" (exact matches only), then the variable should display the text "My Department".

Screenshot of InfoView If-Then variable

Notice that the other fields in our variable are blank. Our variable only said to do something when the condition is met.

Else

Adding "Else do_this" at the end of an If-Then statement tells the variable to do something or display information in the situation where the condition is not met.

Example: If condition_one Then do_this Else do_this

=If [D Dept Code]="ART" Then "My Department"
Else "Not Mine"

This If-Then statement means that if the Department Code is "ART" then show "My Department". However, if the Department Code is not "ART" then show "Not Mine".

Screenshot of InfoView If-Then-Else variable

ElseIf

After the first "Then" statement, another If-Then statement can be added on to the end to create a second conditional statement. Since the new If-Then statement will come after an existing If-Then statement, the "If" turns into "ElseIf"

Example: If condition_one Then do_this ElseIf condition_two Then do_this

=If [D Dept Code]="ART" Then "My Department"
ElseIf [D Dept Code]="ARTH" Then "My Other Department"

This variable states that if the Department Code shows "ART", the variable should show "My Department". If the Department Code shows "ARTH", the variable should show "My Other Department".

Screenshot of InfoView If-Then-ElseIf variable

If-Then-ElseIf statements can also always have an ending "Else" statement.

Example: If condition_one Then do_this ElseIf condition_two Then do_this Else do_this

=If [D Dept Code]="ART" Then "My Department"
ElseIf [D Dept Code]="ARTH" Then "My Other Department"
Else "Not Mine"

This variable will show:

Screenshot of InfoView If-Then-ElseIf-Else variable

If-Then statements can be combined as many times as needed via "ElseIf".

Cautions

If-Then statements can get tricky. Here's a few cautions when using them:

1. If-Then statements can become very complicated very quickly. One If-Then statement written by a UCSC colleague was over two printed pages long! In such cases, logic errors are easy to make. We recommend testing your variable out on a small amount of data in order to verify the accuracy.

2. If you have one row that meets more than one condition in your If-Then-ElseIf statement, it will always do what the first "Then" says and ignore the second.

Example:

=If [D Dept Code]="ART" Then "My Department"
ElseIf [D Dept Code]="ART" Then "My Other Department"

This variable will show:

My Department

Screenshot of InfoView If-Then caution