Article

ASP Language Basics

Page: 1 2 3 4 5 6 Next

Control Structures

Don't let the name scare you; control structures aren't as complicated as they sound! All of the example scripts we've seen so far have had one thing in common: they've executed in a straight line, one ASP statement at a time, from top to bottom. In simple terms, control structures let you break out of that straight-line mould, allowing your programs to react differently to different conditions and saving you some typing when performing repetitive tasks.

Let's start with the most basic control structure, an If...Then statement:

If condition Then statement

If the condition is true, then the statement will be executed. If not, nothing happens and the ASP engine just moves on to the next line of the script. Here's a simple example:

' Check if intA and intB are equal    
Dim blnABEqual    
blnABEqual = (intA = intB)    
if blnABEqual Then Response.Write "A and B are equal!"

This is the practical application of the comparison operators that I promised above! The first line of the above (after the comment) creates a variable called blnABEqual. The next line checks if intA and intB are equal (that is, if they contain the same value). If they are, blnABEqual is assigned a value of true. If not, blnABEqual is set to false. If you printed this variable out, it would display either "True" or "False" on the page, depending on the values of intA and intB. On the last line, we have an If...Then statement that uses blnABEqual as its condition. As you can see, if intA and intB are equal (and thus blnABEqual is true), the program prints out a message to that effect. If not (and blnABEqual is false), then nothing happens.

Since most conditions are only used once, it is more common to type the conditional expression directly in the If...Then statement, instead of using a variable. Here's how the above example would normally look:

' Check if intA and intB are equal    
If intA = intB Then Response.Write "A and B are equal!"

Pretty spiffy, eh? As always, use whichever method you are most comfortable with. Here are a few more examples of comparison operators in action as part of If...Then statements:

If intA > intB Then Response.Write "A is greater than B"    
If intA < intB Then Respone.Write "A is less than B"    
If intA >= intB Then Response.Write "A is greater or equal to B"    
If intA <= intB Then Response.Write "A is lesser or equal to B"    
If intA <> intB Then Response.Write "A does not equal B"

Instead of a single statement, you can specify a whole series of statements that should be executed if the condition is true. You do this by starting the list of statements on a new line and terminating the list with End If. Here's an example:

If blnUserIsKevin Then    
 Response.Write "Hi, Kevin!"    
 Response.Write "How are you today?"    
 ' ... more statements here ...    
End If

Before I continue, a note on coding style. You probably noticed that some of the lines of the above example have spaces in front of them. This coding style, known as indenting is intended to show off the structure of the code. That is, by indenting the statements that will occur as a result of the condition being true by two spaces, it makes clear the fact that they are inside the If...Then statement. The code could have been written without indenting (with every line starting at the left margin), and it would have worked just as well, but the structure would not have been obvious at a glance. When you get to a stage where you're writing complicated scripts, it will not be uncommon for certain passages of your code to be indented five or even ten levels deep (from If...Then statements inside If...Then statements, for example). By picking up this good habit now, you can ensure that your code is easy to read now and in future.

Getting back to If...Else statements, if you want to do something when a condition is false instead of when it is true, there is a handy shortcut you can use. Here's how the code might look without using the shortcut:

' Print message when blnCondition is false    
If blnCondition = False Then    
 Response.Write "blnCondition is false!"    
End If

The = operator (and the <> operator) can be used on Boolean values as well as on numeric types and text strings. As a result, the condition in the If...Then statement above will be true when blnCondition is false, and false when blnCondition is true. The shortcut I mentioned involves something called the negation operator. By placing this operator (NOT) before a variable name or before some expression, the value of that variable or expression will be flipped from true to false (and vice versa). Thus, the above example could be more simply written as follows:

' Print message when blnCondition is false    
If NOT blnCondition Then    
 Response.Write "blnCondition is false!"    
End If

If blnCondition is true, the NOT operator will cause the condition to evaluate to false so the message will not be printed. If blnCondition is false, the NOT flips the expression's value to true and the message does get printed. It's important to note that the NOT operator doesn't actually change the value that is stored in blnCondition; it just alters the value of the condition for the purposes of the If...Then statement.

Another control structure that is related to the If...Then statement is the If...Else statement:

If condition Then    
 ' statements to be executed if condition is true    
Else    
 ' statements to be executed if condition is false    
End If

In this case, one group of statements or the other will be executed. The condition determines which. Often, however, you'll have more than just two groups of statements that you'll want to choose from. By using another If...Else statement in as the second indented block of statements, you can test multiple conditions and perform an action depending on which of the conditions is true:

If condition1 Then    
 ' do this if condition1 is true    
Else    
 If condition2 Then    
   ' do this if condition2 is true    
 Else    
   If condition3 Then    
     ' do this if condition3 is true    
   Else    
     ' do this only if none of the conditions is true    
   End If    
 End If    
End If

Confused? If you wade through the above, you should eventually be able to piece together the three If...Else statements that involved, but you'll probably be happy to know that there is an easier way. Using ElseIf to specify your additional conditions, you can do away with the nested If...Else statements. Here's the previous example reworked using ElseIf:

If condition1 Then    
 ' do this if condition1 is true    
ElseIf condition2 Then    
 ' do this if condition2 is true    
ElseIf condition3 Then    
 ' do this if condition3 is true    
Else    
 ' do this only if none of the conditions is true    
End If

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links