Windows Vista Forums

Is there a 'continue' construct for loops in VBScript
  1. #1


    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

  2. #2


    Bob Barrows [MVP] Guest

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

    Andrew Falanga wrote:

    > 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

  3. #3


    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

    > 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

  4. #4


    ekkehard.horner Guest

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

    Andrew Falanga schrieb:

    > 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

  5. #5


    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:

    > Andrew Falanga schrieb:
    >

    > > 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) ===
    Ok, thanks for the info everyone.

    Andy

      My System SpecsSystem Spec

  6. #6


    Todd Vargo Guest

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

    Andrew Falanga wrote:

    > 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

  7. #7


    Al Dunbar Guest

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


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

    > Andrew Falanga wrote:

    >> 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

  8. #8


    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

    > 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

Is there a 'continue' construct for loops in VBScript problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
are VBscript on Windows server 2003 and VBscript on WS2008 compatible? Francois Lafont VB Script 9 26 Apr 2010
Loops inside Cmdlets Shay Levi PowerShell 10 23 Oct 2007
FYI: 'continue' in 'for' loops is VERY slow Roman Kuzmin PowerShell 5 06 Apr 2007
Traps and Foreach loops lawndart@gmail.com PowerShell 4 14 Feb 2007
Next fucntion in loops? MKielman PowerShell 4 11 Oct 2006