# Using elseif

The IF...THEN...ELSE block is used for testing for two possible outcomes of a condition. The ELSEIF structure is used to test for multiple outcomes. The syntax for an ELSEIF clause is very similar to an IF....THEN structure, as shown in table 1.

Table 1 comparison of IF...THEN and ELSEIF...THEN structures
IF...THEN
`IF intAnyNumber MOD 2 = 0 then`
`response.write intAnyNumber & " is an even number"`
`END IF`
`IF intAnyNumber MOD 2 <> 0 then`
`response.write intAnyNumber & " is an odd number"`
`END IF`
ELSEIF...THEN
`IF intAnyNumber MOD 2 = 0 then`
`response.write intAnyNumber & " is an even number"`
`ELSEIF intAnyNumber MOD 2 <> 0 then`
`response.write intAnyNumber & " is an odd number"`
`END IF`

Notice in table 1 for the ELSEIF structure only one END IF is needed because we only have one IF...THEN structure. An ELSEIF structure does not need an END IF. END IF is used only for ending an IF statement. Why do we need an ELSEIF structure when it is so identical to an IF...THEN structure? These structures may appear identical; however, the difference is in the way they are evaluated. In the case of an ELSEIF structure, once a condition is met all other conditions are not evaluated. In the case of multiple IF...THEN structures each structure will be evaluated.

Let's work with an example to make this distinction. Suppose we want to determine the letter-grade based on a given numerical score. We can assume that 90-100 is an "A", 80-89 is a "B", 70-79 is a "C", 60-69 is a "D", and below 60 is an "F". Table 2 presents the code for the IF...THEN structure and the ELSEIF structure.

Table 2 determining the letter grade with IF...THEN and ELSEIF...THEN structure
IF...THEN structure
`IF intScore >= 90 AND intScore < 100 then`
`response.write "You have an A"`
`END IF`
`IF intScore >= 80 AND intScore < 90 then`
`response.write "You have a B"`
`END IF`
`IF intScore >= 70 AND intScore < 80 then`
`response.write "You have a C"`
`END IF`
`IF intScore >= 60 AND intScore < 70 then`
`response.write "You have a D"`
`END IF`
`IF intScore < 60 then`
`response.write "You have an F"` `END IF`
ELSEIF...THEN structure
`IF intScore >= 90 AND intScore < 100 then`
`response.write "You have an A"`
`ELSEIF intScore >= 80 AND intScore < 90 then`
`response.write "You have a B"`
`ELSEIF intScore >= 70 AND intScore < 80 then`
`response.write "You have a C"`
`ELSEIF intScore >= 60 AND intScore < 70 then`
`response.write "You have a D"`
`ELSEIF intScore < 60 then`
`response.write "You have an F"` `END IF`

Again, an ELSEIF structure has only one ENDIF in table 2. Let's evaluate each of the structure with intScore = 70. In the IF....THEN structure presented in table 2, the first condition is false because intScore is not greater than 90 or less than 100. The next IF...THEN condition is also evaluated to be false because intScore is not greater than 80 or less than 90. The third condition is evaluated to be true because intScore is 70. The next two IF...THEN structures are also checked and each is evaluated to be false.

In comparison, in an ELSEIF structure once a condition is matched remaining conditions, if any, are not checked. Assume again intScore = 70, in the ELSEIF structure, the first, second and third conditions will be checked (as shown in table 2). The fourth and fifth conditions are skipped, however.

Let's do one more example with a different intScore value. If intScore was 90, all the IF...THEN conditions shown in table 2 will be checked. However, in the ELSEIF structure only the first condition would be checked because the first condition is true and the remaining condition will be skipped! Although the performance of these two structures is negligible in this small example, it may become a big performance issue for a complex project that has hundreds or even more possible outcomes. In summary, for performance and for brevity, consider using the ELSEIF structure over individually separated IF...THEN structures.