ASPNL logo (1 kb)
Thursday, November 20, 2008

powered by


Microsoft ASP.NET Connections
Member of ASP Guild
<< previous | index | next >>

Working with choice statements

With choice statements we can either execute a piece of code, or not execute it. If certain conditions are met, the code will be executed, otherwise it won't. There are two kinds of choice statements, If ... Then en Select Case. We will discuss both below.

If ... Then

The If ... Then statement is fairly simple, and can best be explained with an example:

ifthen1.asp
<%
Option Explicit

Dim blDoIt

blDoIt = True
%>
<HTML>
<BODY>
<%
If blDoIt = True Then
   Response.Write "This code will be executed"
Else
   Response.Write "This code will NOT be executed"
End If
%>
</BODY>
</HTML>


In the sample above there are two code blocks, one between If ... Then and Else, and one between Else and End If. The first code block is executed if the expression (a variable or a series of statements that evaluate to a value) blDoIt = True is correct (which is the case here). If that expression is incorrect, then the second code block is executed instead.

Note: In the sample above the name of the variable is preceded by bl. This is a convention showing that we are working with a variable of the sub-type Boolean. A Boolean can have two values, True or False.

If we have more conditions, we can link them together with And or Or. We can also add more ElseIf ... Then statements to have more choices. The example below shows both.

ifthen2.asp
<%
Option Explicit

Dim lngNumber
Dim blDoIt

blDoIt = True
lngNumber = 2
%>
<HTML>
<BODY>
<%
If blDoIt = True And lngNumber = 1 Then
   Response.Write "This code will NOT be executed"
ElseIf blDoIt = True And lngNumber = 2 Then
   Response.Write "This code will be executed"
ElseIf blDoIt = True Or lngNumber = 3 Then
   Response.Write "and this code will NOT be executed"
Else
   Response.Write "This code will NOT be executed"
End If
%>
</BODY>
</HTML>


Select Case

With the If ... Then statement we saw that we could have more choices by using ElseIf ... Then statements. With the Select Case statement we can do something similar, only easier. Here only one expression is given. The result of that expression is compared with the values give for each Case. You can see how this works in the example below:

select1.asp
<%
Option Explicit

Dim lngNumber

lngNumber = 2
%>
<HTML>
<BODY>
<%
Select Case lngNumber
   Case 1
      Response.Write "This code will NOT be executed"
   Case 2
      Response.Write "This code will be executed"
   Case 3
      Response.Write "This code will NOT be executed"
   Case Else
      Response.Write "This code will NOT be executed"
End Select
%>
</BODY>
</HTML>


Note: In the sample above the name of the variable is preceded by lng. This is a convention showing that we are working with a variable of the sub-type Long werken. A Long is a number-type that can have values between 2.147.483.648 and -2.147.483.647.

<< previous | ^ to top | index | next >>
copyright 2000-2002 ASPNL