Decision Making and Looping
by Amrit HallanByteswoth.com
Thursday, 9th March 2006
Looping
Many a time you have to perform a programming task again and again. For example, the program should keep reading the data rows of a database file until it encounters the end of the file. Or, a shopping cart script should keep adding items to the cart until the user clicks on "checkout" button.
Primarily, in VBScript, there are four loop structures, namely, for…next, do…until…loop, while…wend, and do…while…loop. Let us see them one by one…
The for…next loop is typically used when the loop has to happen according to some number, and not a criteria. For instance,
<%
for i = 1 to 15
%>
<p>The current value of i is <%=i%></p>
<%
next
%>
for i = 1 to 15
%>
<p>The current value of i is <%=i%></p>
<%
next
%>
But it doesn't mean the other loop structures cannot handle number-based loops. We can have the above loop with do…until too.
<%
Dim i
i = 1
do until i = 16
%>
<p>The current value of i is <%=i%></p>
<%
i = i+1
loop
%>
Dim i
i = 1
do until i = 16
%>
<p>The current value of i is <%=i%></p>
<%
i = i+1
loop
%>
While loops are somewhat more sophisticated. In while loops, you can decide whether you necessarily want to execute the code within the while loop or not. The below example explains it properly.
<%
' The loop executes at least once, even if the condition is not met with.
Dim CarryOn, i
CarryOn=false
i = 0
do
i = i + 1
loop while CarryOn=true
%>
' The loop executes at least once, even if the condition is not met with.
Dim CarryOn, i
CarryOn=false
i = 0
do
i = i + 1
loop while CarryOn=true
%>
By the end of the loop, the variable i will have value 1 even when its condition of CarryOn being true is not met with. Again,
<%
' The loop executes not even once if the condition is not met with.
Dim CarryOn, i
CarryOn=false
i = 0
do while CarryOn=true
i = i + 1
loop
' another sort of loop
while CarryOn=true
i = i + 1
wend
%>
' The loop executes not even once if the condition is not met with.
Dim CarryOn, i
CarryOn=false
i = 0
do while CarryOn=true
i = i + 1
loop
' another sort of loop
while CarryOn=true
i = i + 1
wend
%>
In the above example, the variable i will contain 0 (zero) because it is in the beginning itself when it is checked what sort of value CarryOn has.
The usage of different while loop structures depends on your preferences and need and you can use most of the loops in any way.
A working example of if-then-else-end-if and looping combined.
In this example, the program counts how many times a particular alphabet appears in a given string. It also highlights the found alphabet in the string.
<%
Dim LongString, AlphToFind, AlphCounter
' Suppose the alphabet to find is e
LongString = "In the heavy downpour, everything was swept away and we had to swim through the rushing currents to reach the next village."
HighlightedString=""
AlphToFind = "e" ' Usually, this value can be entered by some user using an online form.
AlphCounter = 0 ' Initially it should have zero value.
' we'll use the For...Next loop
for i=1 to len(LongString)
if mid(LongString, i, 1) = AlphToFind then
HighlightedString = HighlightedString & "<strong>" & mid(LongString, i, 1) & "</strong>"
AlphCounter = AlphCounter + 1
else
HighlightedString = HighlightedString & mid(LongString, i, 1)
end if
next
%>
<p>The letter '<%=AlphToFind%>' appears <%=AlphCounter%> times in the long string "<%=HighlightedString%>"</p>
Dim LongString, AlphToFind, AlphCounter
' Suppose the alphabet to find is e
LongString = "In the heavy downpour, everything was swept away and we had to swim through the rushing currents to reach the next village."
HighlightedString=""
AlphToFind = "e" ' Usually, this value can be entered by some user using an online form.
AlphCounter = 0 ' Initially it should have zero value.
' we'll use the For...Next loop
for i=1 to len(LongString)
if mid(LongString, i, 1) = AlphToFind then
HighlightedString = HighlightedString & "<strong>" & mid(LongString, i, 1) & "</strong>"
AlphCounter = AlphCounter + 1
else
HighlightedString = HighlightedString & mid(LongString, i, 1)
end if
next
%>
<p>The letter '<%=AlphToFind%>' appears <%=AlphCounter%> times in the long string "<%=HighlightedString%>"</p>
You may notice some new string-handling functions - len() and mid() - but we shall discuss them at the appropriate time.
Options:
Printer Friendly
Email Friend
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.
