Control-of-FLOW language elements in ASP,
If-statements
IF condition THEN instruction that has to be done when true ELSE instruction that has to be done when false END IFExample:
age=25 IF age<18 THEN response.write("Non-adult") ELSE response.write("Adult") END IFWith nesting you'll have to use ELSEIF
IF condition1 THEN instruction that has to be done when first condition true ELSEIF condition2 THEN instruction that has to be done when second condition true ELSE instruction that has to be done when false END IFSelect or Multiple choice-statement
SELECT CASE variable CASE value1 instruction CASE value2 instruction CASE ELSE else-instruction END SELECT
variable Name of the variable that has to be tested value1, value2,... A value for the test instruction if the variable=value then the instruction is executed else-instruction If the value of the variable is not one of the values listed the this instruction is executed Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example on select-case</TITLE></HEAD> <BODY> <% theday=Weekday(now()) response.write('The day of the week is "& theday &"<HR>") SELECT CASE theday CASE vbSunday response.write("Sunday") CASE vbMonday response.write("Monday") CASE vbTuesday response.write("Tuesday") CASE vbWednesday response.write("Wednesday") CASE vbThursday response.write("Thursday") CASE vbFriday response.write("Friday") CASE vbSaturday response.write("Saturday") CASE ELSE response.write("Another day") END SELECT %> </BODY> </HTML>FOR ... NEXT-statement
There are 2 versions of FOR, the classic one and FOR EACH.
FOR (clasic)
FOR variable= startvalue TO endvalue STEP stepvalue instruction NEXTStep value is not obligated, if you don't add it, the value is added 1 at each next instruction
Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example on For</TITLE></HEAD> <BODY> <% FOR counter1= 1 TO 3 response.write("<br>("& counter1 &")") NEXT %> </BODY> </HTML>For each...next-statement
This instruction is executed for every element in a table or group.
FOR EACH element IN group instruction NEXT
element The name of the variable that contains the present value of the element from the group group The name of the table or group instruction The instruction that has to be executed Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example on For</TITLE></HEAD> <BODY> <% FOR EACH entry IN Request.form response.write( entry &" : "& request.form(entry)) response.write("<br>") NEXT %> </BODY> </HTML>This gets each entry and its value in a form (on a HTML-page)
Do while/until...loop-statement
Do while ... loop
DO WHILE condition instruction LOOPWhen the condition is true the loop continuous. When the condition is false the loop is broken and the program continious.
A typical example is to get every element from a table(database).
DO WHILE not rs.Eof response.write rs("name") response.write ("<br>") rs.movenext LOOPIt is important you don't forget rs.movenext or you have a endless loop.
A DO WHILE ... LOOP will not be executed if the condition is immidiatly false.
A DO ... LOOP UNTIL will at least be executed once.
Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example do until</TITLE></HEAD> <BODY> counter1=0 DO counter1=counter1+1 response.write(counter1) LOOP UNTIL counter1=10 %> </BODY> </HTML>