VBScript: Multidimensional Arrays, Statements, and Commenting - Conditional Statements
(Page 3 of 6 )
Just like other programming languages, VBScript has Conditional Statements. In their simplest form, these work on the following basis: if this is true, do that. VBScript has three conditional statements, starting with the...
If Statement
As stated above, the If Statement says: if this is true, do that. A real life example would be: "If mom yells that my Star Wars Spaghetti-Os are finished cooking, leave my super cool basement nerd pad to eat them."
Here it is in some code:
<html>
<body>
<script type="text/vbscript">
Dim sitcom
sitcom = "ALF"
If sitcom = "Alf" Then
document.write("Don't eat my cat!")
End If
</script>
</body>
</html>
The above code created a variable named sitcom and assigns it the value "Alf." It then performs an If statement that says if the value of sitcom is Alf, then print out some text. If the value of sitcom had not been Alf, nothing would have happened. Since the criteria was met however, this would be the result:
Don't eat my cat!
That's all fine and dandy if the criteria is met, but what if it wasn't? What if we wanted to respond if the value of sitcom had not been Alf? For that we would use the Else Clause.
You Better or....Else
When people say that it's never a good thing. No one ever says things like "You better clean your room or else I will do it for you while you eat these warm brownies I just baked." Instead its always stuff like "You better quit calling me, sending me notes, and hiding in the bushes outside my window with those binoculars or I will inform the police that you are not following the restraining order I had to put on you because you couldn't handle that I left you for your cousin Tina at your seventeenth birthday party." Women...
Here is some code showing the Else clause at work:
<html>
<body>
<script type="text/vbscript">
Dim sitcom
sitcom = "Mister Belvedere"
If sitcom = "Alf" Then
document.write("Don't eat my cat!")
Else
document.write("Hey change the channel, Alf is on!")
End If
</script>
</body>
</html>
The above code creates the variable sitcom and adds the value, "Mister Belvedere." It then creates an If statement that asks if the value of sitcom is "Alf." If it is, it will print: Don't eat my cat! to the screen. If not, will print the following:
Hey change the channel, Alf is on!
Next: If...Then...Elseif >>
More BrainDump Articles
More By James Payne