An IF/ELSE block can be used when you want to execute some code when a condition is true and some other code when the condition is false. The IF...THEN...ELSE block is used for testing for two possible outcomes of a condition. In other words, by adding an else clause to the simple IF statement you add an alternative action if the condition is not met. If the condition is met, statement(s) inside an ELSE clause is not executed.
To execute some code when the condition is false, we need to use an ELSE clause. An ELSE clause cannot be used alone; it must be used with an IF statement. The following is the general syntax for an IF/ELSE structure:
IF condition(s) THENASP Statement(s) for when the condition is metELSEASP Statement(s) for when the condition is NOT metEND IF
Analysis of the above code should point out that either:
Consider the following IF/THEN/ELSE structure as an example:
dim intGradeintGrade = 50IF intGrade >= 70 THENresponse.write "Passing grade."ELSEresponse.write "Failing grade."END IF
In this example, we start by declaring a variable called intGrade and we next initialize it to the numerical value of 50. Next, we check if the value of this variable is greater than or equal to 70 with the statement IF intGrade >= 70 THEN. Because 50 is not greater than or equal to 70, the condition is not satisfied. Therefore, only the statement inside the ELSE clause will be executed. Our ELSE clause simply contains a print out statement: "Failing grade." So "Failing grade." will be printed to the screen. If, however, the value of intGrade is 72 (or higher or just 70) in the same example, then, the before statement after THEN and before ELSE is executed, specifically, the message "Passing grade." The following shows graphically of what is happening when using an IF/ELSE structure:
|
As is depicted in the illustration above, one of the two possible messages will be printed. Either "Passing grade." or "Failing grade." will be printed once the script is executed.
The following example shows what happens when the condition is met inside your IF/ELSE structure:
Dim intEvenTestDim intEvenTest = 94IF intEvenTest MOD 2 = 0 THENresponse.write intEvenTest & " is an even number"ELSEresponse.write intEvenTest & " is an odd number"END IF
In this example we are testing if the number 94 is even or odd. Our IF intEvenTest MOD 2 = 0 THEN statement uses the MOD operator to compute the remainder after dividing 94 by 2. If this operator returns 0, then we know it is an even number. Otherwise, the number is odd. Because the MOD operator will return 0 when 94 is divided by 2, our condition is satisfied. Therefore, "94 is an even number" will be printed. Had the number been odd, the statement inside the ELSE clause would have been executed.
Again, the code inside the IF statement is executed only if the condition is true. If the condition is false and if you are using an ELSE clause, then, the code inside the ELSE clause is executed. In the following if intAnyNumber is 3, then
IF intAnyNumber MOD 2 = 0 THENresponse.write intAnyNumber & " is an even number"ELSEresponse.write intAnyNumber & " is an odd number"END IF
"3 is an odd number" is displayed because 3 MOD 2 is not equal to 0.
Instead of having an ELSE clause, we could use two separate IF...THEN clauses for evaluating each condition. Suppose intAnyNumber is 3, the following example provides the same results but notice no ELSE clause is used:
IF intAnyNumber MOD 2 = 0 THENresponse.write intAnyNumber & " is an even number"END IFIF intAnyNumber MOD 2 <> 0 THENresponse.write intAnyNumber & " is an odd number"END IF
However, performance-wise it is not efficient because two conditions will be checked instead of one. If you have multiple conditions to check, which is normally what happens in a real web application, it is not suggested to use separate simple IF structures for each possible condition. When possible, try testing all possible conditions with as few IF statements as possible. For example, avoid:
dim intGrade2intGrade2 = 0IF intGrade2 >= 70 THENresponse.write "Passing grade."END IFIF intGrade2 < 69 THENresponse.write "Failing grade."END IFIF intGrade2 < 68 THENresponse.write "Failing grade."END IF...IF intGrade2 < 1 THENresponse.write "Failing grade."END IF