Decision Making and Looping
by Amrit HallanByteswoth.com
Thursday, 9th March 2006
Decision Making
VBScript too can take if-then-else decisions. An example:
<%
Dim num1, num2
num1=45
num2=33
if num1<num2 then
%>
<p><%=num1%> is less then <%=num2%></p>
<%
elseif num1=num2 then
%>
<p><%=num1%> is equal to <%=num2%></p>
<%
else
%>
<p><%=num1%> is greater than <%=num2%></p>
<%
end if
%>
Dim num1, num2
num1=45
num2=33
if num1<num2 then
%>
<p><%=num1%> is less then <%=num2%></p>
<%
elseif num1=num2 then
%>
<p><%=num1%> is equal to <%=num2%></p>
<%
else
%>
<p><%=num1%> is greater than <%=num2%></p>
<%
end if
%>
Side Note: In the above program, we already know the values of num1 and num2, but if a user enters these values using an HTML form, then only this sort of program will be able to tell which value is greater.
We can also check for multiple conditions, for instance, "if this is Jack's room and Jack is in the room and the room is in his house then Jack is at home." Programmatically:
<%
Dim JacksRoom, RoomOfJacksHouse, JackInRoom, JackAtHome
JacksRoom=true
JackInRoom=true
RoomOfJacksHouse=true
if JacksRoom=true and JackInRoom=true and RoomOfJacksHouse=true then
JackAtHome=true
Else
JackAtHome=false
End if
%>
Dim JacksRoom, RoomOfJacksHouse, JackInRoom, JackAtHome
JacksRoom=true
JackInRoom=true
RoomOfJacksHouse=true
if JacksRoom=true and JackInRoom=true and RoomOfJacksHouse=true then
JackAtHome=true
Else
JackAtHome=false
End if
%>
So for JackAtHome to be true, all preceding variables must be true too. So if we put an AND, all the conditions have to satisfy the "if" condition.
If Jack can be at home if even one of the conditions is true, then we can have
<%
if JacksRoom=or JackInRoom=or RoomOfJacksHouse=true then
JackAtHome=true
End if
%>
if JacksRoom=or JackInRoom=or RoomOfJacksHouse=true then
JackAtHome=true
End if
%>
In case of an OR, any one of the conditions can satisfy an if structure.
You can see that the if-then-else-end-if statement is almost similar to the real-world decision-making structure, and hence, is quite self-explanatory. Just take care that each if-then-else-end-if is properly indented, so that you can have multiple sub-if-then-else-end-if's without getting confused or bogged down.
These are also called logical structure, for, if a and b are not equal, then some condition being a surely means that the condition is not b.
Similar to if-then-else-end-if is the Select Case structure, which is much better organized then the if structures. Let's see how. (Note that whenever we have to insert remarks in the VBScript code, it is preceded by a single-quote.)
<%
' Assuming a visitor chooses a particular food recipe to read and assume the value is being stored in a variable called recipe. So we use the Select Case in the following manner:
Select Case recipe
Case "Egg Curry" ' recipe contains "Egg Curry"
' take the appropriate action
Case "Mango Pie" ' recipe contains "Mango Pie"
' take the appropriate action
Case Else ' All other recipes have been checked for but the one left
' take the appropriate action
End Select
%>
' Assuming a visitor chooses a particular food recipe to read and assume the value is being stored in a variable called recipe. So we use the Select Case in the following manner:
Select Case recipe
Case "Egg Curry" ' recipe contains "Egg Curry"
' take the appropriate action
Case "Mango Pie" ' recipe contains "Mango Pie"
' take the appropriate action
Case Else ' All other recipes have been checked for but the one left
' take the appropriate action
End Select
%>
Continue to the next for Looping.
Options:
Printer Friendly
About The Author:
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at amrit@bytesworth.com . For further details, visit http://www.bytesworth.com You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com.
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at amrit@bytesworth.com . For further details, visit http://www.bytesworth.com You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at bytesworth-subscribe@topica.com.
