Web Hosting Canada
Business Web Hosting  
Free Domain Registration  
Call Toll-Free 1-866-421-8886  
 Dedicated Server Hosting
DEVELOPER RESOURCES


 • 
Technetex Canada


 • 
Support Website


 • 
Forums


 • 
Dev Resources


ASP


 • 
ASP Reference Index


 • 
Built-in ASP Objects


 • 
Other ASP Objects


 • 
VBScript Reference


 • 
Links


 

   
 
Control Structures previous | next
 

Control structures allow you to control the flow of execution of your scripts. You can specify that some code should be executed only under certain circumstances, using conditional structures. You can specify that some code should be executed repeatedly, using looping structures. Lastly, you can specify that code from somewhere else in the script shoul be executed using branching structures.

 
Conditional Structures
 

If...Then...Else allows you to choose which block of code to execute based on a condition or series of conditions.

If condition1 Then
  code block 1
ElseIf condition2 Then
  code block 2
Else
  code block 3
End If

If condition1 is True, code block 1 is executed. If it is False, and condition2 is True, code block 2 is executed. If condition1 and condition2 are False, code block 3 executes.
An If...Then construct may have zero or more ElseIf statements, and zero or one Else statements.
You can use NOT to reverse the logic of the IF structure. (i.e. If Not...Then...Else)

Select Case can be used in stead of using some really complex If...Then constructs.

Select Case variable
Case chioce1
  code block 1
Case choice2
  code block 2
...
Case choiceN
  code block N
Case Else
  default code block
End Select

This compares the value of variable with choice1, choice2, and so on. If it finds a match, it executes the code block associated with that choice. If it does not find a match, it executes the default code block.

top
Looping Structures

Looping structures allow you to execute the same block of code repeatedly. The number of times it executes may be fixed or may be based on one or more conditions. Be careful not to create an endless loop; that is one where the condition required to break the loop is never met.

 

For...Next looping structures take the following form:

For counter = start to stop
  code block
Next

code block is executed with counter having the value start, then again with counter having the value start+1, then start+2, and so on until it has the value stop. It then leaves the loop.

Optionally, you may specify a different value to increment counter by using this construct:

For counter = start to stop Step stepvalue
  code block
Next

counter will take the values start+stepvalue, for example if stepvalue is 2 counter will count by 2s. If stepvalue is negative, stop should be less than start.

For Each...Next loops use the following form:

For Each item In set
  code block
Next

code block is executed with item taking the value of each member of set. set should be an array or a collection.

Do...While looping structure:

Do While booleanValue
  codeblock
Loop

code block is executed as long as booleanValue is True. If it is False to begin with, the loop is not executed at all.

While...Wend looping structures have the following form :

while booleanValue
  code block
Wend

code block is executed as long as booleanValue is True. If it is False to begin with, the loop is not executed at all. Note: Wend is not supported in ASP.NET; use End While instead.

Do...Loop While looping structure:

Do
  code block
Loop While booleanValue

code block is executed as long as booleanValue is True. The loop is executed at least once no matter what.

Do Until...Loop looks like this:

Do Until booleanValue
  code block
Loop

code block is executed as long as booleanValue is False. If it is True to begin with, the loop is not executed at all.

Do...Loop Until uses the following form:

Do
  code block
Loop Until booleanValue

code block is executed as long as booleanValue is False. The loop is executed at least once no matter what.

top
Branching Structures
Branching structures allow you to jump from one position in the code to another.
 

Subroutines do not return a value. They simply execute.

Sub name (argumentlist)
  code block
End Sub

Functions do return a value and have the following form:

Function name (argumentlist)
  code block
name = expression
End Function

  top
 
Logo
2004 © Technetex
Protected by Copyright. All Rights Reserved.