# \<if>

## \<if> <a href="#if" id="if"></a>

The `<if>` element is used for conditional logic. It has optional `<else>` and `<elseif>` elements.

### Attributes

<table><thead><tr><th width="134.33333333333331">Name</th><th width="177">Data type<select><option value="027671a5361d493aa38b444f5aba5cfc" label="String" color="blue"></option><option value="7fcb007e91384c37becdf626789123f6" label="Integer" color="blue"></option><option value="ffa7910e0cdf455187d1da736f2cb146" label="Boolean" color="blue"></option></select></th><th>Description</th></tr></thead><tbody><tr><td>cond</td><td><span data-option="027671a5361d493aa38b444f5aba5cfc">String</span></td><td>An ECMAScript expression to evaluate.</td></tr></tbody></table>

### Notes

Note that the `<else>` and `<elseif>` tags both must contain a trailing forward slash to terminate.

{% tabs %}
{% tab title="Correct example" %}

```markup
<if cond="number==5">
  <!-- do something -->
<elseif cond="number==2"/>
  <!-- do something else -->
<else/>
  <!-- do something for all other cases -->
</if>
```

{% endtab %}

{% tab title="Incorrect example" %}

```markup
<if cond="number==5">
  <!-- do something -->
<elseif cond="number==2">
  <!-- this won't work since we left off the / -->
<else>
  <!-- this also won't work since we left off the / -->
</if>
```

{% endtab %}
{% endtabs %}

### Example

```markup
<?xml version="1.0"?>
<vxml version="2.0">
  <form>
    <field name="myprompt" type="digits">
      <prompt>
        Say the magic number between 1 and 9.
      </prompt>
      <filled>
        <!-- The "cond" attribute within an "if" tag -->
        <!-- should contain a boolean ECMAscript expression. -->
        <if cond="myprompt==5">
          <prompt>
            You guessed correctly!
          </prompt>
        <elseif cond="myprompt==4 || myprompt==6"/>
          <prompt>
            You're close. Try again.
          </prompt>
          <clear namelist="myprompt"/>
        <else/>
          <prompt>
            Try again.
          </prompt>
          <clear namelist="myprompt"/>
        </if>
      </filled>
    </field>
  </form>
</vxml>
```

The output of the above script would be:

> Computer: Say the magic number between one and nine.\
> Human: Three.\
> Computer: Try again. Say the magic number between one and nine.\
> Human: Six.\
> Computer: You're close. Try again. Say the magic number between one and nine.\
> Human: Five.\
> Computer: You guessed correctly!

### Child Tags

[\<audio>](https://docs.plumvoice.com/dev/voicexml/tags/audio-tag), [\<assign>](https://docs.plumvoice.com/dev/voicexml/tags/assign-tag), [\<clear>](https://docs.plumvoice.com/dev/voicexml/tags/clear-tag), [\<data>](https://docs.plumvoice.com/dev/voicexml/tags/data-tag), [\<disconnect>](https://docs.plumvoice.com/dev/voicexml/tags/disconnect-tag), [\<else>](https://docs.plumvoice.com/dev/voicexml/tags/else-tag), [\<elseif>](https://docs.plumvoice.com/dev/voicexml/tags/elseif-tag), [\<enumerate>](https://docs.plumvoice.com/dev/voicexml/tags/enumerate-tag), [\<exit>](https://docs.plumvoice.com/dev/voicexml/tags/exit-tag), [\<foreach>](https://docs.plumvoice.com/dev/voicexml/tags/foreach-tag), [\<goto>](https://docs.plumvoice.com/dev/voicexml/tags/goto-tag), [\<if>](https://docs.plumvoice.com/dev/voicexml/tags/if-tag), [\<log>](https://docs.plumvoice.com/dev/voicexml/tags/log-tag), [\<prompt>](https://docs.plumvoice.com/dev/voicexml/tags/prompt-tag), [\<reprompt>](https://docs.plumvoice.com/dev/voicexml/tags/reprompt-tag), [\<return>](https://docs.plumvoice.com/dev/voicexml/tags/return-tag), [\<script>](https://docs.plumvoice.com/dev/voicexml/tags/script-tag), [\<submit>](https://docs.plumvoice.com/dev/voicexml/tags/submit-tag), [\<throw>](https://docs.plumvoice.com/dev/voicexml/tags/throw-tag), [\<value>](https://docs.plumvoice.com/dev/voicexml/tags/value-tag), [\<var>](https://docs.plumvoice.com/dev/voicexml/tags/var-tag)

### Parent Tags

[\<block>](https://docs.plumvoice.com/dev/voicexml/tags/block-tag), [\<catch>](https://docs.plumvoice.com/dev/voicexml/tags/catch-tag), [\<error>](https://docs.plumvoice.com/dev/voicexml/tags/error-tag), [\<filled>](https://docs.plumvoice.com/dev/voicexml/tags/filled-tag), [\<foreach>](https://docs.plumvoice.com/dev/voicexml/tags/foreach-tag), [\<help>](https://docs.plumvoice.com/dev/voicexml/tags/help-tag), [\<if>](https://docs.plumvoice.com/dev/voicexml/tags/if-tag), [\<noinput>](https://docs.plumvoice.com/dev/voicexml/tags/noinput-tag), [\<nomatch>](https://docs.plumvoice.com/dev/voicexml/tags/nomatch-tag)
