Lesson 6 | ASP Decision Structure Statements |
Objective | Create programming structures in ASP scripts. |
ASP Decision Structure Statements
If/Then/Else Decision Structure
One of the most useful and most often-used statements in coding ASP scripts is the decision structure, a familiar structure in programming. The essential syntax is:
However it can also be written as
If logical–expression Then
Statement1
Else
Statement2
End If
Notice that an End If
statement is used with the extended form. Here is an example that puts one of two messages in a variable to be printed. This will come in handy for our course project:
<HTML>
.
.
<%
Dim cQuote 'Initialize a character variable
If nTotal > 100
Then
cQuote = "Amount spent is greater than 100"
Else
cQuote = "Amount spent is less than 100"
End If
%>
<!–END ASP SCRIPT ––>
.
.
.
A duplicate of the actual IF
statement was placed as a remark behind the End If
statement. In long scripts this makes it easy to keep track of where the End If
began
IF-ELSE-THEN
You may want to indent the actions after the THEN and the ELSE statements.
This makes for easier reading of yourscript.
Select/Case
Where you have more than two items to compare against you might use a Select/Case structure.
Select and Case Structure in ASP
As mentioned in the lesson text, the Select/Case
structure can contain as many Case tests as needed. All comparison operators can be used. The colon at the end of each Case
statement is optional.
Here is the syntax:
Select Case variable
Case value1:
<statement>
Case value2:
<statement2>
Case value3:
<statement3>
Case value4:
<statement4>
Case Else:
<statement>
End Select
One use of this structure in our T-shirt store might be to set the value of a character variable to the spelled-out shirt size based upon a one-letter code input by the user.
Here is a code segment that accomplishes this:
<HTML>
.
.
<% 'BEGIN ASP SCRIPT
Select Case cSizeAbbrev
Case "S"
cSizeFull = "Small"
Case "M"
cSizeFull = "Medium"
Case "L"
cSizeFull = "Large"
Case "XL"
cSizeFull = "Extra-Large"
Case Else
cSizeFull = "Unknown"
End Select
%>
<!–END ASP SCRIPT ––>
.
.
</HTML>
This spelled-out text in the variable can be used later to print a receipt or display a message to the user.
The Select/Case
structure can contain as many Case tests as needed, and all comparison operators can be used. This structure is quite flexible, and is often used for sequential testing of a variable against a number of categories or test values.
The For/Next loop
The essential syntax for this structure is:
For loopVar = beginValue To endValue
<statements>
Next
Whenever loopVar is less than beginValue or greater than endValue, the program flow goes directly from the
For
statement to the Next
statement, skipping any statements between them.
Otherwise, the <statements> are executed over and over and loopVar is increased by 1 each time the For/Next
code is looped through.
Do/Loop and While/Wend
The For/Next
structure is used when the starting and ending values are known.
When they are not known, you will use the Do/Loop
or While/Wend
structure.
Here's the basic syntax for Do/Loop
:
Do
<statements>
Loop While | Until expression is true
For While/Wend:
While expression is true
<statements>
Wend expression is false
Wend
is equivalent to End While, the program resuming execution with any statement following it. We will use the While/Wend
structure in an exercise later in the course. The next lesson will present ASP functions for data manipulation.