Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista Tutorial - Is there a 'continue' construct for loops in VBScript

Reply
 
Old 10-09-2008   #1 (permalink)
Andrew Falanga
Guest


 
 

Is there a 'continue' construct for loops in VBScript

Hi,

As you might have guessed, both from this post and the one I made
yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
cannot find a construct such as 'continue' for loops. I'm assuming
that many in this forum are familiar with languages like C or Java and
will recognize this keyword.

I need a way to start at the top of a For Each loop if a specific
condition is met, but can't seem to find Continue. I shall keep
looking while awaiting replies.

Thanks for any help,
Andy

My System SpecsSystem Spec
Old 10-09-2008   #2 (permalink)
Bob Barrows [MVP]
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript

Andrew Falanga wrote:
Quote:

> Hi,
>
> As you might have guessed, both from this post and the one I made
> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
> cannot find a construct such as 'continue' for loops. I'm assuming
> that many in this forum are familiar with languages like C or Java and
> will recognize this keyword.
>
> I need a way to start at the top of a For Each loop if a specific
> condition is met, but can't seem to find Continue. I shall keep
> looking while awaiting replies.
>
Stop looking. Change the logic in your loop so that a continue statement
isn't needed. Typically an If statement is needed:
for ...
if ... then
'do something
elseif ... then
'do something else
else
'do nothing - essentially continuing the loop
end if
next

Select Case can also be used.

--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


My System SpecsSystem Spec
Old 10-09-2008   #3 (permalink)
mayayana
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript

Does it have to be For/Next? In a For/Next
loop you can use Exit For:

For Each OFile in oFiles
If oFile.Name = "SomethingOrOther.txt" then exit for

'-- other ops here

Next

If you can use a Do loop then you have other options.
There are While and Until keywords. (Look it up
in the WSH CHM help file.) There's also Exit Do:

Do While x = 0

'-- ops here.
Loop

Do
If x = 0 then Exit Do
'-- ops here.
Loop
Quote:

> As you might have guessed, both from this post and the one I made
> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
> cannot find a construct such as 'continue' for loops. I'm assuming
> that many in this forum are familiar with languages like C or Java and
> will recognize this keyword.
>
> I need a way to start at the top of a For Each loop if a specific
> condition is met, but can't seem to find Continue. I shall keep
> looking while awaiting replies.
>
> Thanks for any help,
> Andy

My System SpecsSystem Spec
Old 10-09-2008   #4 (permalink)
ekkehard.horner
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript

Andrew Falanga schrieb:
Quote:

> Hi,
>
> As you might have guessed, both from this post and the one I made
> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
> cannot find a construct such as 'continue' for loops. I'm assuming
> that many in this forum are familiar with languages like C or Java and
> will recognize this keyword.
>
> I need a way to start at the top of a For Each loop if a specific
> condition is met, but can't seem to find Continue. I shall keep
> looking while awaiting replies.
>
> Thanks for any help,
> Andy
In C you can use "continue" in a loop to restrict the code following
the "continue" statement to special cases:

#include <stdio.h>

int main( void )
{
int nIdx;
for( nIdx = 0; nIdx <= 5; ++nIdx )
{ printf( "%d is evil (as all numbers are)\n", nIdx );
if ( 2 != nIdx )
{ continue;
}
printf( "but %d is especially mean and hates %d\n", nIdx, nIdx + 1 );
++nIdx;
}
return 0;
}

output:

evilnums.exe
0 is evil (as all numbers are)
1 is evil (as all numbers are)
2 is evil (as all numbers are)
but 2 is especially mean and hates 3
4 is evil (as all numbers are)
5 is evil (as all numbers are)

in VBScript you can use an If statement with reverted condition:

Dim nIdx
For nIdx = 0 To 5
WScript.Echo nIdx, "is evil (as all numbers are)"
If 2 = nIdx Then
WScript.Echo "but", nIdx, "is especially mean and hates", nIdx + 1
nIdx = nIdx + 1
End If
Next

output:

=== continueVBS: continue in VBScript
0 is evil (as all numbers are)
1 is evil (as all numbers are)
2 is evil (as all numbers are)
but 2 is especially mean and hates 3
4 is evil (as all numbers are)
5 is evil (as all numbers are)
=== continueVBS: 0 done (00:00:00) ===

My System SpecsSystem Spec
Old 10-09-2008   #5 (permalink)
Andrew Falanga
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript

On Oct 9, 10:10*am, "ekkehard.horner" <ekkehard.hor...@xxxxxx>
wrote:
Quote:

> Andrew Falanga schrieb:
>
Quote:

> > Hi,
>
Quote:

> > As you might have guessed, both from this post and the one I made
> > yesterday, I'm a VBScript neophyte. *I'm looking on MSDN but, as yet,
> > cannot find a construct such as 'continue' for loops. *I'm assuming
> > that many in this forum are familiar with languages like C or Java and
> > will recognize this keyword.
>
Quote:

> > I need a way to start at the top of a For Each loop if a specific
> > condition is met, but can't seem to find Continue. *I shall keep
> > looking while awaiting replies.
>
Quote:

> > Thanks for any help,
> > Andy
>
> In C you can use "continue" in a loop to restrict the code following
> the "continue" statement to special cases:
>
> * * *#include <stdio.h>
>
> * * *int main( void )
> * * *{
> * * * *int nIdx;
> * * * *for( nIdx = 0; nIdx <= 5; ++nIdx )
> * * * *{ printf( "%d is evil (as all numbers are)\n", nIdx );
> * * * * *if ( 2 != nIdx )
> * * * * *{ continue;
> * * * * *}
> * * * * *printf( "but %d is especially mean and hates %d\n", nIdx, nIdx + 1 );
> * * * * *++nIdx;
> * * * *}
> * * * *return 0;
> * * *}
>
> output:
>
> * * *evilnums.exe
> * * *0 is evil (as all numbers are)
> * * *1 is evil (as all numbers are)
> * * *2 is evil (as all numbers are)
> * * *but 2 is especially mean and hates 3
> * * *4 is evil (as all numbers are)
> * * *5 is evil (as all numbers are)
>
> in VBScript you can use an If statement with reverted condition:
>
> * *Dim nIdx
> * *For nIdx = 0 To 5
> * * * *WScript.Echo nIdx, "is evil (as all numbers are)"
> * * * *If 2 = nIdx Then
> * * * * * WScript.Echo "but", nIdx, "is especially mean and hates", nIdx + 1
> * * * * * nIdx = nIdx + 1
> * * * *End If
> * *Next
>
> output:
>
> * *=== continueVBS: continue in VBScript
> * *0 is evil (as all numbers are)
> * *1 is evil (as all numbers are)
> * *2 is evil (as all numbers are)
> * *but 2 is especially mean and hates 3
> * *4 is evil (as all numbers are)
> * *5 is evil (as all numbers are)
> * *=== continueVBS: 0 done (00:00:00) ===
Ok, thanks for the info everyone.

Andy
My System SpecsSystem Spec
Old 10-09-2008   #6 (permalink)
Todd Vargo
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript

Andrew Falanga wrote:
Quote:

> Hi,
>
> As you might have guessed, both from this post and the one I made
> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
> cannot find a construct such as 'continue' for loops. I'm assuming
> that many in this forum are familiar with languages like C or Java and
> will recognize this keyword.
>
> I need a way to start at the top of a For Each loop if a specific
> condition is met, but can't seem to find Continue. I shall keep
> looking while awaiting replies.
It sounds like either you want to Exit from the loop or are looking for the
Next statement.

For Each ...
If condition = True Then Exit For
Next

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec
Old 10-10-2008   #7 (permalink)
Al Dunbar
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript


"Todd Vargo" <tlvargo@xxxxxx> wrote in message
news:edzFuNlKJHA.4600@xxxxxx
Quote:

> Andrew Falanga wrote:
Quote:

>> Hi,
>>
>> As you might have guessed, both from this post and the one I made
>> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
>> cannot find a construct such as 'continue' for loops. I'm assuming
>> that many in this forum are familiar with languages like C or Java and
>> will recognize this keyword.
>>
>> I need a way to start at the top of a For Each loop if a specific
>> condition is met, but can't seem to find Continue. I shall keep
>> looking while awaiting replies.
>
> It sounds like either you want to Exit from the loop or are looking for
> the
> Next statement.
>
> For Each ...
> If condition = True Then Exit For
> Next
Sounds more like he wants to abort the current iteration and go directly to
the next one, not actually exit the loop entirely. I think that is what the
"continue" construct does in those other languages he mentions.

Bob Barrows had the best solution, however it reflects only one model. Here
is the generic continue:

for each ...
do some stuff before deciding
if A continue (i.e. start the next iteration of the loop now)
do some more stuff
next

This maps to:

for each ...
do some stuff before deciding
if not A then
do some more stuff
end if
next

It gets more complicated if the choice to continue is nested more deeply in
some other loop of if block... I agree with Bob on the approach: don't try
to map a continue'd structure in a language lacking continue... think the
problem through a different way that uses only the available constructs.

/Al


My System SpecsSystem Spec
Old 10-12-2008   #8 (permalink)
SteveSinclair
Guest


 
 

Re: Is there a 'continue' construct for loops in VBScript


"Andrew Falanga" <af300wsm@xxxxxx> wrote in message
news:17cf1f2b-2394-44b6-a691-27c518813b43@xxxxxx
Quote:

> Hi,
>
> As you might have guessed, both from this post and the one I made
> yesterday, I'm a VBScript neophyte. I'm looking on MSDN but, as yet,
> cannot find a construct such as 'continue' for loops. I'm assuming
> that many in this forum are familiar with languages like C or Java and
> will recognize this keyword.
>
> I need a way to start at the top of a For Each loop if a specific
> condition is met, but can't seem to find Continue. I shall keep
> looking while awaiting replies.
>
> Thanks for any help,
> Andy
Another approach would be to use a nested Do...Loop Until True

For Each obj_TargetSubFolder in col_TargetSubFolders
Do
' some stuff goes here
If (Mid(obj_TargetSubFolder.Name,1,1) = "$") Then Exit Do: ' Equates to a
continue for the For

' more stuff goes here
Wscript.Echo obj_TargetSubFolder.Name

Loop Until True: ' So the Do..Loop
Next

--
Steve

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Loops inside Cmdlets PowerShell
Task manager loops back? Vista General
FYI: 'continue' in 'for' loops is VERY slow PowerShell
Traps and Foreach loops PowerShell
Next fucntion in loops? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46