Constructing IF statements

If you have ever done any programming, you probably be familiar with constructing a simple if statement because it is one of the most common programming control structures used. If you do not have programming experience or are new to ASP, this page will show you how to get started with constructing IF statements in ASP. In the context of programming, constructing of conditions involves conditional expression (or simply one or more conditions), and an action, if any.

Whether or not you have realized, in our daily lives we use if statements all the times. For example, you may say if I am thirsty, I want to drink a glass of water. Our condition in this case is "if I am thirsty." The action is drinking glass of water. Notice here our action is dependent on the satisfaction of the condition. Similarly, the action that our program will perform will depend on the outcome of the conditional expression. A conditional expression simply evaluates either to true (condition being met) or false (condition is not met).

Fortunately, constructing conditions in ASP using VBScript is very straight forward process because it uses English language like words. The following shows the general syntax for constructing an IF statement:

IF condition(s) THEN
ASP Statement(s)

The IF, THEN, and END IF are all keywords of VBScript in ASP. One or more conditions are placed after the keyword IF. If more than one condition is used, a logical operator (i.e., AND, OR, or NOT) must be used to connect the conditions. After the condition, the keyword THEN must be used to end the listing of condition(s) and to list the statements that need to be processed if the condition is met. The END IF keyword indicates the end of an IF..THEN block.

Let's do a simple example of finding an even number. If a number is divisible by 2 and the remainder is 0, then it is an even number. Because 2, 4, 6, 8, and 10 are all divisible by 2 and their remainder is 0, these are even numbers. A MOD operator can be used to test the condition that if a number is even. A MOD operator returns the remainder of two numbers. We can test for an even number by using an IF..THEN statement:

IF intAnyNumber MOD 2 = 0 THEN
response.write intAnyNumber & " is an even number"

In this example, we constructed a condition that checks if a number (or the value of the variable intAnyNumber) is even. If the number is even, we print out a message saying the number is even. If the number is not even (in other words, if the condition is not met), the response.write statement is not executed. Consequently, nothing will be printed to the screen. The following illustration summarizes this example:

Constructing an IF statement for finding an even number
Illustrating showing an IF statement to determine if a number is even

In the following program we determine if the number 100 is even using a condition similar to what we used above:

DIM intOneHundred
intOneHundred = 100
IF intOneHundred MOD 2 = 0 THEN
response.write intOneHundred & " is an even number"

In this example, we start by declaring a variable called intOneHundred. In the next line, we assign the value 100 to intOneHundred, using the assignment operator. Next, we write an IF statement to check if the result of the MOD operator equals 0. If the result of this operation is 0, then, the number, in this case 100, must be an even. If our IF statement is satisfied, then, we print a message indicating 100 is even. The END IF statement ends the IF condition. What would be the result of this program? Because 100 is divisible by 2, our IF condition will be satisfied, as shown here:

100 is an even number

If it helps, you may start constructing a condition in words before writing the actual programming code:

If (number is even) then
Print number is even
End if

That may also be helpful if you have to write complex conditions. Because an IF..THEN statement is a most commonly used conditional logic structure, it is worth understanding how it constructed and how it works.