INT213 – Week 3
Conditional Statements and Looping
Welcome
- Overview for this week
- Using Control Structures
- Labs 1 and 2 must be done by Thursday of this week
- Resource: ASP Editors
- Don’t program in Notepad!!!! Grab a developer’s editor:
- SourceEdit -
- Notepad2 -
Conditional Statements
- If…Then…End If
- Requires a Boolean Expression, and executes the script code contained within the If… block only if the Boolean Expression evaluates to True, for example:
If x = 4 Then
Response.Write “The value is 4”
End If
- TIP: Notice that all lines after the If but before the End If are indented 1 tab stop. Always indent your If…Blocks so that it is clear that the code contained within is part of the condition of the If.
- Using OR and AND to chain expressions
- OR allows you to check whether 1 of 2 expressions are true. Both don’t have to be true at the same time.
- AND allows you to check that both of 2 expressions are true at the same time.
- Complex Expressions
- You can create a complex expression by having each term of an expression also be an expression itself. Consider:
If x = 4 Then
If x = 4 And y = 5 Then
If (x = 4 And y = 5) Or z = 3
If (x = 4 And y = 5) Or (z = 3 Or b = 14)
and so on…
- TIP: If your expression gets really long, consider breaking it onto separate lines, for example:
If (x = 4 And y = 5) Or _
(z = 3 Or b = 14)
- VBScript Operators
- = (equal)
- returns TRUE if values are equal
If 1 = 1 Then ‘ TRUE
Dim x
x = 6
If x = 1 Then ‘ FALSE
- > (greater than)
- returns TRUE if first value is greater than the second
If 3 > 2 Then ‘ TRUE
Dim x
x = 4
If x > 4 Then ‘ FALSE
- < (less than)
- returns TRUE if first value is less than the second
If 2 < 3 Then ‘ TRUE
Dim x
x = 5
If x < 4 Then ‘ FALSE
- >= (greater than or equal to)
- returns TRUE if first value is greater than or equal to the second
If 3 >= 3 Then ‘ TRUE
If 3 >= 2 Then ‘ TRUE
Dim x
x = 4
If x >=3 Then ‘ FALSE
- <= (less than or equal to)
- returns TRUE if first value is less than or equal to the second
If 2 <= 3 Then ‘ TRUE
If 3 <= 3 Then ‘ TRUE
Dim x
x = 5
If x <= 4 Then ‘ FALSE
- Not (the negative operator, used to negate an expression)
- Not is used when you want to flip the meaning of an expression to produce its opposite, for example, testing that a value on a form is not one of two predefined values:
Dim choice
choice = Request.Form(“txtValue”)
If Not (choice = “One” Or choice = “Two”) Then
‘ Choice could equal “Three” for example
End If
- Nested If…Then
- Often you have conditions which are themselves part of other conditions:
If y > 0 Then
If y >= 1 And y <= 4 Then
Response.Write “y is between 1 and 4”
End If
End If
- TIP: Notice how the IF blocks are again indented, but nested IF blocks get indented a second level. Always indent your IF block code according to its level of nesting within other blocks.
- If…Then…ElseIf…Else…End If
- If you want to evaluate code based on multiple conditions, you can use Nested Ifs, but also you can use ElseIf and Else
- ElseIf is used when you have a further condition to your If, which you expect might also occur:
If x = 1 Then
‘ Code for case 1
End If
If x = 2 Then
‘ Code for case 2
End If
Vs.
If x = 1 Then
‘ Code for case 1
ElseIf x = 2 then
‘ Code for case 2
End If
- Else is used when you want to catch all other possible values without specifying them:
If x = 1 Then
‘ Code for case 1
ElseIf x = 2 then
‘ Code for case 2
Else
‘ Code for case 3, 4, 5, …
End If
- Select Case…End Select
- When you have a lot of test conditions, using IF can become tedious. Select Case is used to simplify such situations:
Select Case Request.Forms(“course_code”)
Case “INT213”, “WIN200”
Response.Write “Windows Course”
Case “OPS234”
Response.Write “Linux Course”
Case “DCN286”
Response.Write “Networking Course”
Case Else
Response.Write “Unknown Course”
End Select
- You can separate multiple cases with commas to indicate that they should be grouped together.
- Case Else is a catch-all which will be run if no other cases matched the expression.
Loops
- Loops allow us to repeat steps, and to do so an indefinite number of times
- There are really two families of loops:
- Loops that run a Specified number of times, which includes the For…Next and For Each…Next loops. (NOTE: we’ll return to For Each later in the course)
- Loops that run an Unspecified number of times, which includes the rest of the VBScript loops. These Loops can be further broken down into:
- Loops that run At Least Once
- Do…Loop While, Do…Loop Until
- Loops that run Zero or more times
- Do While…Loop, Do Until…Loop, For
- For…Next
- When you know exactly how many times your loop should be run (i.e., “know” might mean you have a variable), you can use a For…Next loop to cut down on the amount of code you have to write:
Dim i ‘ Loop counters are usually named i, j, k, etc.
For i = 0 to 10
Response.Write i & “<br />”
Next i
- By default the For loop uses a Step of 1, which means that every time it gets to Next i, it adds 1 to the value of i. You can change this using the Step keyword with a positive (e.g., 2 to count by twos) or negative (e.g., -1 to count backwards) value. For example, to print the numbers from 10 down:
Dim i
For i = 10 to 0 Step -1
Response.Write i & “<br />”
Next i
- Do While…Loop and Do…Loop While
- The Do While loops run while some expression is True. The two versions differ in terms of where the expression is checked: at the top or bottom of the loop. This makes a difference because when the expression is checked at the top of the loop, it might mean that the loop never runs (i.e., if the expression is false from the beginning).
‘ Add all numbers between two values
Dim first, second, total
first = Request.Form(“txtFirstValue”)
second = Request.Form(“txtSecondValue”)
total = 0
Do While first <= second
total = total + first
first = first + 1
Loop
Response.Write “The total is “ & total
- Do Until…Loop and Do…Loop Until
- The Do Until loops run until some expression is True—basically the opposite of Do While. With Do While loops, you are saying, “Do something as long as the following condition stays true.” With Do Until you are saying, “Do something until a condition is true.” The two versions differ in terms of where the expression is checked: at the top or bottom of the loop. This makes a difference because when the expression is checked at the top of the loop, it might mean that the loop never runs (i.e., if the expression is false from the beginning).
Dim first, second, total
first = Request.Form(“txtFirstValue”)
second = Request.Form(“txtSecondValue”)
total = 0
Do
total = total + first
first = first + 1
Loop Until first = second
Response.Write “The total is “ & total