This chapter describes:
- "If" Statements
- "If" Statement Example
- "Select Case" Statements
- "Select Case" Statement Example
"If" Statements
There 4 flavors of "If" statements in VB:
1. Single-statement "If":
If condition Then statement
where "condition" is Boolean value, and "statement" is any VB statement. The specified statement will be executed, if and only if the specified condition is "True".
2. Multi-statement "If":
If condition Then
statement_block (multiple statements)
End If
The specified multi-statement block will be executed, if and only if the specified condition is "True".
3. "If ... Else" Statement:
If condition Then
statement_block_a
Else
statement_block_b
End If
Two statement blocks are specified. But only one statement block will be executed based on the value of the specified condition. If the specified condition is "True", the first block will be executed. If the specified condition is "False", the second block will be executed.
4. "If ... ElseIf" Statement:
If condition_1 Then
statement_block_1
ElseIf condition_2 Then
statement_block_2
...
...
Else
statement_block_n
End If
Many statement blocks are specified. But only one statement block will be executed based on the value of the condition specified for that block.
"If" Statement Example
To help you understand how "If" statements work, I wrote the following the example, condition_if.html:
<html>
<body>
<!-- condition_if.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
' Single-statement "If"
bIsNewYear = True
document.writeln("")
If bIsNewYear Then document.writeln("Happy New Year!")
' Multi-statement "If"
bIsLogOn = True
document.writeln("")
If bIsLogOn Then
document.writeln("Open the log file.")
document.writeln("Write the log message.")
document.writeln("Close the log file.")
End If
' "If ... Else" statement
bIsAdmin = False
document.writeln("")
If bIsAdmin Then
document.writeln("Display the delete button.")
document.writeln("Display the edit button.")
Else
document.writeln("Display the view button.")
End If
' "If ... ElseIf" statement
iDay = 4
If iDay = 0 Then
sOpenHours = "closed"
ElseIf iDay >= 1 And iDay <=5 Then
sOpenHours = "open from 9:00am to 9:00pm"
ElseIf iDay = 6 Then
sOpenHours = "open from 9:00am to 5:00pm"
Else
sOpenHours = "undefined"
End If
document.writeln("")
document.writeln("The library is " & sOpenHours)
</script>
</pre>
</body>
</html>
Here is the output:
Happy New Year! Open the log file. Write the log message. Close the log file. Display the view button. The library is open from 9:00am to 9:00pm
The output matches my expectation perfectly!
"Select Case" Statements
"Select Case" statement is an alternative way of executing statements selectively based on certain conditions. Here is the syntax of "Select Case" statements:
Select Case test_value
Case expected_value_list_1
statement_block_1
Case expected_value_list_2
statement_block_2
...
...
Case Else
statement_block_n
End Select
where "test_value" is a value used to test against with expected values specified in the "Case" clauses, and "expected_value_list_*" are lists of expected values. The execution of "Select Case" statement goes like this:
- Each "Case" clause will be visited sequentially from top to bottom.
- If one of the values in the expected value list equals to the test value in a "Case" clause, the statement block in that clause will be executed.
- If the statement block of any "Case" clause gets executed, the entire statement ends.
- The "Case Else" clause is a special clause. Its statement block will be executed if and only if no statement block of any other "Case" clauses gets executed.
"Select Case" Statement Example
To help you understand how "Select Case" statements work, I wrote the following the example, condition_case.html:
<html>
<body>
<!-- condition_case.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
iDay = 4
Select Case iDay
Case 0
sDay = "Sunday"
sOpenHours = "closed"
Case 1, 2, 3, 4, 5
sDay = "a weekday"
sOpenHours = "open from 9:00am to 9:00pm"
Case 6
sDay = "Saturday"
sOpenHours = "open from 9:00am to 5:00pm"
Case Else
sDay = "unkown"
sOpenHours = "undefined"
End Select
document.writeln("")
document.writeln("Today is " & sDay)
document.writeln("The library is " & sOpenHours)
</script>
</pre>
</body>
</html>
Here is the output:
Today is a weekday The library is open from 9:00am to 9:00pm
Hope you know how to use "Select Case" statements now.
Conclusions
Remember that:
- "If" must be followed by a condition and "Then".
- "ElseIf" is one word, no space.
- "Case" is followed by a list of expected values.
- Unlike C language, no need to break statements in "Case" clauses.
discuss this topic to forum
