Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Continue While statement VBSCRIPT

2,061 views
Skip to first unread message

Mohit Gupta

unread,
Feb 24, 2004, 5:26:06 PM2/24/04
to
HI all,

I have question about VBSCRIPT WHILE statement. When using while statement, I want to re-iterate through the while statement without executing other instrustions once one of my conditions gets true/False

e.g

DO WHILE I < 5
IF I= 3 THEN
'skip all the instructions below just like 'continue' keyword in c,c++, java or jscript
'How to do this
END IF

'Otherwise print the value of I
Response.Write I & "<BR>"
LOOP


Joe Earnest

unread,
Feb 24, 2004, 6:02:36 PM2/24/04
to
Hi,

"Mohit Gupta" <anon...@discussions.microsoft.com> wrote in message
news:554842F8-CEF6-42B2...@microsoft.com...

The Exit statements from a loop are the closest things to effecting a
"continue" statement. Faux loops (i.e. loop structures that are defined to
exit without actually looping (i.e., do ... loop until true, or for i= true
to true ... next) can be used to similar effect. For your current issue:

do while (i<5)
if (i=3) then exit do
Response.Write I & "<BR>"
loop

Joe Earnest

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.564 / Virus Database: 356 - Release Date: 01-19-04


Mohit Gupta

unread,
Feb 24, 2004, 6:11:10 PM2/24/04
to
Hi jon,

Thanks for providing me the answer. However, the solution you have provided will break the do..while loop structure completely. I don't want to do that. If 'I' value becomes equal to 3 then I want to simply skip that iteration and proceed over to the next iteration.

Thanks
Mohit

Richard Mueller [MVP]

unread,
Feb 24, 2004, 6:17:31 PM2/24/04
to
Mohit Gupta wrote:

Possibly:

DO WHILE I < 5
IF I= 3 THEN
'skip all the instructions below just like 'continue' keyword in c,c++,
java or jscript
'How to do this

ELSE


'Otherwise print the value of I
Response.Write I & "<BR>"

END IF
LOOP

--
Richard
Microsoft MVP Scripting and ADSI
HilltopLab web site - http://www.rlmueller.net
--


Joe Earnest

unread,
Feb 24, 2004, 6:45:34 PM2/24/04
to
Hi,

"Mohit Gupta" <anon...@discussions.microsoft.com> wrote in message

news:B47F1B64-ABB3-4FC7...@microsoft.com...

Sorry, misunderstood what you wanted to do. Richard's If block does it, or,
to give a couple of ways, including an example of a faux loop:

do while (i<5)
if NOT (i=3) then Response.Write I & "<BR>"
loop


do while (i<5)
do


if (i=3) then exit do
Response.Write I & "<BR>"

loop until true
loop

Mohit Gupta

unread,
Feb 24, 2004, 7:01:10 PM2/24/04
to

Thanks Joe

I have already thought about the second solution but I am pretty much unstatisfied with that. You know I really wanted to be something like 'Continue' keyword in Java or C++ so that I don't have to write IF...ELSE...END IF every where. Microsoft should have provided an equivalent of 'Continue' keyword in the VB arena.

Now consider example below.

sqlQuery = "query"
Recordset.Open sqlQuery, DBCONnn

WHILE NOT recordset.EOF
val = recordset.Fields("field1")
IF CINT(val) = 0 THEN
recordset.moveset
continue
END IF

IF CINT(val) <5 THEN
'write to database
recordset.Movenext
CONTINUE
END IF
WEND

With 'CONTINUE" keyword I found it easier to code. Now If I would have gone for Richard example, then I would have to write IF...ELSE...END IF everywhere.

And If go for Faux loop, then I would have to write while...WEND statement twice (I think this is bit unsatisfactory)

Any comments are appreciated.

Thanks
Mohit Gupta
----- Joe Earnest wrote: -----

Joe Earnest

unread,
Feb 24, 2004, 7:40:38 PM2/24/04
to
Hi,

"Mohit Gupta" <anon...@discussions.microsoft.com> wrote in message

news:4E6037F2-09D6-49F6...@microsoft.com...

Preference for syntax is largely a matter of taste, and I've learned that it
doesn't pay to argue it. To me, the Exit statements are more versatile for
various situations than is Continue (which is just a short form of a nested
loop), but I have a long history in BASIC syntaxes. Remember that you don't
have to block If's in VBS. Single-line If statements and Do-Loops are
available. A couple of variations:

do while (i<5)
do until (i=3)


Response.Write I & "<BR>"
loop
loop


do while (i<5): do until (i=3)
Response.Write I & "<BR>"
loop: loop

do while (i<5)
do until (i=3): Response.Write I & "<BR>": loop
loop


do while (i<5)
if NOT (i=3) Response.Write I & "<BR>"
loop

BTW, for multiple exit conditions, the niftiest faux loops are For's,
because they can automatically supply a flag to test for exit or completion
of an isolated routine, using the final increment on the Next statement:

for flag=true to true
...
if ... then exit for
...
next
'flag=true if exits, false if not

for flag=false to false step true
...
if ... then exit for
...
next
'flag=false if exits, true if not

Joe Earnest

Joe Earnest

unread,
Feb 24, 2004, 8:06:54 PM2/24/04
to
oops (left out a Then) ...

| do while (i<5)
| if NOT (i=3) Response.Write I & "<BR>"
| loop

should be

do while (i<5)
if NOT (i=3) then Response.Write I & "<BR>"
loop

---

Michael Harris \(MVP\)

unread,
Feb 24, 2004, 10:42:56 PM2/24/04
to
> Now consider example below.
>
> sqlQuery = "query"
> Recordset.Open sqlQuery, DBCONnn
>
> WHILE NOT recordset.EOF
> val = recordset.Fields("field1")
> IF CINT(val) = 0 THEN
> recordset.moveset
> continue
> END IF
>
> IF CINT(val) <5 THEN
> 'write to database
> recordset.Movenext
> CONTINUE
> END IF
> WEND

WHILE NOT recordset.EOF
DO


val = recordset.Fields("field1")
IF CINT(val) = 0 THEN
recordset.moveset

EXIT DO 'continue
END IF

IF CINT(val) <5 THEN
'write to database
recordset.Movenext

EXIT DO 'continue
END IF
EXIT DO:LOOP
WEND

--
Michael Harris
Microsoft.MVP.Scripting

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scriptcenter/scrguide/sagsas_overview.asp

TechNet Script Center Sample Scripts
http://www.microsoft.com/technet/scriptcenter/default.asp
Download in HTML Help format (searchable)
http://www.microsoft.com/downloads/release.asp?ReleaseID=38942

WSH 5.6 documentation download
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

Al Dunbar [MS-MVP]

unread,
Feb 29, 2004, 5:03:10 PM2/29/04
to

"Mohit Gupta" <anon...@discussions.microsoft.com> wrote in message
news:554842F8-CEF6-42B2...@microsoft.com...

Pardon my late entry into this thread, but here is one way I might do this:

DO WHILE ( I < 5 )
IF ( I <> 3 ) THEN


'Otherwise print the value of I
Response.Write I & "<BR>"

END IF
LOOP

No need for an else clause or a NOT i=3 as others have suggested, these add
unnecessary extraneousness to the code. Also, the WHILE-WEND loop is
generally deprecated because it covers only one of the special cases handled
by DO-LOOP.

I have always felt that a CONTINUE construct was too much like a GOTO (i.e.:
GOTO terminal statement of this loop structure). Even if available, it does
not allow the structured use of indenting to make it plain what is being
done conditionally (or not done) under certain circumstances.

If there is a possibility of multiple cases where the code should be
"skipped", perhaps a select case block:

DO WHILE ( I < 55 )
select case I
case 3, 5, 7, 9, 11 ,15, 27, 39
' skip
case else


'Otherwise print the value of I
Response.Write I & "<BR>"

end select
LOOP


/Al


0 new messages