![]() | |||||||||||||||||||||
![]() |
|
|
Loop-the-Loop
Active Server Pages contains various loops that can be used to process data. These loops include:
If-Then-Else Loop
We will now discuss each of these loop types individually.
If-Then-Else Loop
This loop allows for checking of code, if a statement is found then certain processing occurs, otherwise something else is processed.
In this example if the variable "name" is equal to the value "Joe" then the "Hello Joe!" string is outputted. Otherwise, the script will output "I don't think we have met."
For-Next Loop
The For-Next loop allows for code to be processed until a specific goal is reached. We have already seen one example of the for-next loop. Here is another example:
This example outputs the value of "i" 20 times, each time incrementing it once. This would produce:1234567891011121314151617181920. This allows for code to be run a multiple of times very easily.
At any time in a For-Next Loop the loop can exit using an "Exit For" statement.
In this example when "i" equals "y" the For-Next Loop will not continue and the rest of the page will process.
For-Each-Next Loop
The For-Each-Next Loop is specifically designed for arrays, collections, or dictionaries. This allows for processing to occur on each value in one of these types of situations. If you had an array, for example, you could process for each value in the array.
The "Exit For" statement works the same way in this situation as it does in the previous For-Next Loop.
Do-While/Until Loop
The Do-While/Until Loop allows for bits of code to be run Until a goal is attained or While a goal is not attended.
This outputs Hi There!<BR> to the page each time incrementing "x". Once x has reached the value of "10" the loop is broken and the page continues processing. Do-Until loops basically work the same way, but instead of proceding through the loop while a value remains true, it procedures through the loop until something becomes true.
At any time in a Do-While/Until Loop the loop can exit using an "Exit Do" statement.
In this example when "x" equals "y" the loop will not continue and the rest of the page will process.
While-Wend Loop
The final loop type we will discuss is the While-Wend. The While-Wend loop will process given statements until a goal or condition is achieved. This type of loop is very similar to the Do-While Loop and generally not thought of as structured as the Do-While Loop.
This outputs Hi There!<BR> and increments "x". Once x has reached the value of "10" the While-Wend loop is broken and the page continues processing.
|
Section 2: Loop-the-Loop Section 3: Make a Statement Section 4: Final Results
| ||||||||||||||||||||||