Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The While Loop - Page 2

March 5, 2001

To loop through our array, we would normally use a For...Next loop and loop until our counter was equal to the upper boundary, or last element, of our array. UBound() is a function that can be performed on an array to calculate this value. Even though we are not using a For...Next loop we will use UBound nonetheless.

While loops are basically Do...Until loops without the Do...Until part. Rather, using the keywords While and Wend to block off the loop, we can achieve the same effect. There is no real advantage to this alternative, sometimes it just makes more sense conceptually, but it is completely up to you how and when to use it. Let's take a look at how we would use a While loop along with UBound() function to loop through the elements of the array and store the survey results.

<%
	Dim surveyResults()
	Dim numResults
	Dim counter
	'a variable that counts
	'for us.

	Dim result
	'a variable that stores
	'user-input results for us
	'these results are then
	'moved into the array.

	numResults = 12

	ReDim Preserve surveyResults(numResults)

	counter = 0
	While counter < surveyResults.UBound()
		'pretend there is a user prompt here
		'it could be a long list of form fields
		'a prompt you build, whatever.
		'For now we're just concerned with how
		'arrays work.

		result = "True"
		'some value - either "True" or "False"
		'for this example.

		surveyResults(counter) = result
		counter = counter + 1
		'increment counter so loop isn't infinite
	Wend
%>

I inserted a few extras in there but the comments in the code should explain what they are. What is important is how the While loop takes a counter and compares it to the UBound or length of the array. As long as this counter is not greater then the result of UBound() performed on the array, then the code in between the While and Wend will be executed.

As noted in the code, I am assuming that the results are somehow coming into this page. This is not an example you can just throw on your Web server and use, it is mainly conceptual in nature. For now just try to understand what the array and loops are doing.

One last thing to note about this snippet is the following line, surveyResults(counter) = result. This line is where the contents of result are moved into the array at element position counter. On the first time through the loop, this position will be equivalent to surveyResults(0). On the last iteration, counter will be eleven, thus surveyResults(11). Once it has reached this point, we do not want to tell the computer to try surveyResults(12), because that would be outside the length of the array (remember it is zero-based) so we stop the loop there, as the condition counter < surveyResults.UBound() will return false. Just so you can see, I will show you the more sensible way to approach this with a For...Next loop. Check it out then move on to the next page where we will discuss objects.

<%
	Dim surveyResults()
	Dim numResults

	Dim result
	numResults = 12

	ReDim Preserve surveyResults(numResults)

	For X = 0 to surveyResults.UBound()
		'pretend there is a user prompt here
		'it could be a long list of form fields
		'a prompt you build, whatever.
		'For now we're just concerned with how
		'arrays work.

		result = "True"

		surveyResults(X) = result
		Next
%>

Back to Basics: VBScript for ASP - Part 2
Back to Basics: VBScript for ASP - Part 2
Working with Objects - Page 3


Up to => Home / Authoring / ASP / VBScript




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers