1.
|
IF condition THEN statement
|
2.
|
IF condition THEN
statements
END IF
|
3.
|
IF condition THEN
statements
ELSE
statements
END IF
|
|
|
Example:
If hour( time() ) < 12 then
response.write "Good
morning"
else
response.write "Good afternoon"
End If
|
4.
|
IF condition1 THEN
statements
ELSEIF condition2 THEN
statements
ELSEIF condition3 THEN
statements
ELSE
statements
END IF
|
5.
|
DO WHILE condition
statements
LOOP
Most frequent use:
- Looping through database recordsets
- Often don't know how many records there
are
Caution:
Dim iLoop
iLoop = 1
Do while iLoop > 0
response.write
"Loop=" & iLoop
iLoop = iLoop + 1
Loop
|
6.
|
FOR iCounter = 1 TO n [STEP m]
statements
NEXT
Example:
Dim decCounter, decTo, decStep
decTo = Request.querystring("decTo")
decStep = Request.querystring("decStep")
For decCounter=1 to decTo STEP decStep
Response.Write "decCounter =" & _
decCounter & "<br>"
Next
|
|
Modifiers
-
NOT
-
AND, OR
-
Parentheses ()
Example 1:
Do While intYear > 1999 AND _
strStudentName >
""
Response.write strStudentName & intGradYear
Loop
Example 2:
If NOT strPassword = "Sandvig" then
response.cookies("myCookie")("ID") =
"OK"
End If
Example 3:
If strPass = "fire" OR strPass =
"water" then
response.cookies("myCookie")("ID") =
"OK"
End If
Parentheses
- Use Parentheses to force order of
execution
- Improve readability
- IF NOT true and false and true or
false then
- IF NOT (true and false) and (true or
false) then
- Try to avoid complex logic
- Break down into smaller parts
|
|
|
|
|
|
Other ASP Control Structures
-
Can create custom:
-
Subroutines
-
Functions
-
Components
-
Useful for code used more than once
-
Modularization
-
Encapsulation
-
Subroutines
-
Perform some action
-
Example & syntax:
-
Functions
-
Return a result
-
Example:
-
Response.write "Root of 2 = " & Sqr(4)
- Can also use server-side includes
- No encapsulation.
- Use on many pages
- Subroutines within SSI
- Benefits of SSI & encapsulation
|
|
|
|