AMPscript: IF – PART 1 – THE CONDITIONAL STATEMENT

One of the most used and most powerful AMscript functions out there is the IF statement. It is what allows us to set different levels on a single parameter, show or hide chunks of code, and more. I find if we break it down into 2 parts that it is easier to grasp.

The IF Function simplified is:

IF condition is true
THEN do something
ELSE
THEN do something else
ENDIF

So today we are going to focus on that first part – the conditional statement. These do not have to be big long complicated things it could just be asking “Are all the pencils blue?”. If we look at the picture above we can see that there is a yellow pencil so the answer would be no. In the case of AMPscript, it is running each one as a separate check -> “Pencil is blue” and would get (top to bottom): True, False, True, True, True, True.

When we talk about conditional statements – you want to think of them as a True / False question. The examples below have a value for both sides of the equation to make it easier for you to process.

Conditional statements are easy when we are working with numbers as it is the same as math rules – equals (==), does not equal (!=), greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=)

Lets try some size comparisons ( > or < )

  1. 10 > 5
  2. 1.0 > 5
  3. 7 < 9
  4. 9 >= 7
  5. 2 >= 3

Let’s do a few more – this time looking at the equals —

Note: If you only have 1 “=” sign then AMPscript will try and setting a value on a variable, so when trying to compare something you will need to have “==” denoting it a comparison.

  1. 4 == 3 + 1
  2. 4 == 3 * 1
  3. 4 * 3 == 6 * 2
  4. 12 – 8 == 2 * 2
  5. 6 + 3 == (2*4) – (3 *2)

I know what you are thinking – When am I going to use this — The answer is all the time. Let’s take a real-world example. (Yep you guessed it, more math only time it’s a word problem)

The company offers a rewards program. If a user has 2500 pts they can get $5 off, and at 4500 they get $10 off.

Since many if statements that are used in email, set a value for that email entry and then uses that as one side of the equation, lets plug in some users. Below is the balance for our users. Take a look at each of the three statements and decide if they are T or F for that entry.

For example: Sarah is 1. True, 2. False, 3. False

Sarah=3000
Mike=12000
Carla=700
Jane=778
Lucy=0

  1. X has enough points to receive a $5 reward
  2. X needs to earn 1800 more to get $5 off
  3. X will have 4000 pt left after using a $10 reward

You can also use “AND” and “OR”

  1. Sarah and Mike qualify for a reward
  2. Sarah and Carla qualify for a reward
  3. Sarah or Carla qualify for a reward

You can also use conditional statements with strings with equals (==) and not equal (!=)

  1. Sarah != Carla
  2. Yellow == YELLOW
  3. lowercase(“bLUe”)==lowercase(“blue”)
  4. “Yellow Pencil” == “Yellow Pencils”

There are lots more comparison examples I could pick from. But the big thing to remember is that code does not comprehend comparisons as concepts. If you say “orange” = “fruit”, concept wise is that true but the code looks at the two strings and says that “orange” does not have the same combination of characters as the string “fruit”.

Check out Part 2 next week – to see what these comparisons tied to if() statement can do.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.