eWebProgrammer eweb

ASP   «Prev  Next»
Lesson 6ASP Decision Structure Statements
ObjectiveCreate 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:

If logical-expression  Then 

However it can also be written as
If logical–expression Then
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:

  Dim cQuote  'Initialize a character variable
  If nTotal > 100 
    cQuote = "Amount spent is greater than 100"
    cQuote = "Amount spent is less than 100"
End If

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


You may want to indent the actions after the THEN and the ELSE statements.
This makes for easier reading of yourscript.


Where you have more than two items to compare against you might use a Select/Case structure
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

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:

Loop While | Until expression is true

For While/Wend:

While expression is true
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.