# IF Function

The IF function allows you to perform conditional processing (not to be confused with the IIF function which is a simplified version).

The IF function can be written with the following statements:

• IF – (required) – Begin the IF conditionals with this statement
• ELSEIF – (optional) – You can use the ELSEIF statement for conditional “else if” processing. Multiple ELSEIF statements can be used.
• ELSE – (optional, can only be used once) – If all other statements are not satisfied, default to this else conditional
• ENDIF – (required) – All IF conditionals need to end with the ENDIF statement.

## Example 1:

Using just the ELSE conditional statement:

```                    ```

%%[
set @test = “abc”

IF @test == “abc” THEN
set @response = “Hello, world!”
ELSE
set @response = “Goodbye”
ENDIF
]%%
%%=v(@response)=%%
```
```

## Output:

`Hello, world!`

## Example 2:

Using ELSEIF statements:

```                    ```

%%[
set @test = “abc”

IF @test == “123” THEN
set @response = “Hello, world!”
ELSEIF @test == “abc” THEN
set @response = “Goodbye!”
ENDIF
]%%
%%=v(@response)=%%
```
```

## Output:

`Goodbye!`

## Explanation of Example 1:

Using IF conditional statements allows the statements to be evaluated depending on what is within the statements. Multiple ELSEIF statements can be used within the conditional statement. Must be opened with IF and closed with ENDIF.

In the first example, the variable `@test` is set as the string of “abc” and we’re using the `IF` function to evaluate the `@test` variable. Since `@test` does indeed equal to “abc” (since we set it in the line before) the `IF` function evaluates it as true and then sets the `@response` variable to equal “Hello, World!”

But if we used a different string in the first example, say, `set @test = "notabc"` then the `IF` statement would have evaluated `@test == "abc"` as false and therefore would have then defaulted to the `ELSE` statement resulting in the output of “Goodbye.” This is depicted in Example 1A below:

## Example 1A:

Same code as Example 1 above but with a different string value for the `@test` variable.

```                    ```

%%[
set @test = “notabc”

IF @test == “abc” THEN
set @response = “Hello, world!”
ELSE
set @response = “Goodbye”
ENDIF
]%%
%%=v(@response)=%%
```
```

## Output:

`Goodbye`

## Explanation of Example 2:

Example 2 is similar to Example 1A above but using an `ELSEIF` statement. An `ELSE` statement can be also added to example 2 to have a default value return if none of the conditions hold true. Know that AMPscript evaluates from top to bottom so if any one of those conditions are met starting from the top, the `IF` statement will not evaluate anything below it. For example, let’s expand on Example 2 with more `ELSEIF` statements:

## Example 2B:

Using `ELSEIF` statements and also adding an `ELSE` at the end before the closing `ENDIF`:

```                    ```

%%[
set @test = “abc”

IF @test == “123” THEN
set @response = “Hello, world!”
ELSEIF @test == “abc” THEN
set @response = “Goodbye!”
ELSEIF @test == “abc123” THEN
set @response = “Is this thing on?”
ELSEIF @test == “aaaaaa” THEN
set @response = “Anyone home?”
ELSE
set @response = “Nothing found”
ENDIF
]%%
%%=v(@response)=%%
```
```

## Output:

`Goodbye!`

Note that the output is still “Goodbye!” and then rest of the `ELSEIF` statements are ignored since the condition has already been met since `@test` does equal to “abc”. The `ELSE` statement is also not evaluated because AMPscript reads from top to bottom and the condition has already been met before reaching the `ELSE` statement.