|
||
Lesson 6
Objective
|
ASP Decision Structure Statements Create programming structures in ASP scripts. |
|
| 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:
If logical-expression Then statement1 Else statement2
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
The For/Next loop
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 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.
Do/Loop and While/Wend
Otherwise, the <statements> are executed over and over and loopVar is increased by 1 each time the For/Next code is looped through. 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
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.
|
||
|
|
||
