Re: Exiting a for loop

From: Joshua Frank (jfrank.removeluncheonmeat_at_archimetrics.com)
Date: 09/01/04


Date: Wed, 01 Sep 2004 13:22:34 -0400

This is the ONE place where I think GoTo is better than the alternative:

For i As Integer = 1 To 10
        'code here...
        if i = 3 then GoTo Continue

        'code here...
Continue:
Next

Continue is a pretty fundamental need, and they're finally adding it in
VS 2005, but meanwhile I simulate it as shown above.

Microsoft wrote:
> I have a for loops that goes from 1 to 256
>
>
> I test the number and exception is thrown if there is an error.
>
> How do I get the program to stop in the middle of the loop and go to the
> next increment if an exception is thrown. I can only figure out how to exit
> the loop.
>
> 'read the text file
>
> line = sr.ReadLine()
>
> Do While Not line Is Nothing
>
> subnet = line.Remove(line.Length - 1, 1)
>
> For ip = 1 To 256
>
> computer = subnet & ip
>
> ' Connect to IP and send results
>
>
>
> Dim scope As New ManagementScope("\\" & computer & "\root\cimv2")
>
> Dim objectQuery As New ObjectQuery("select * from Win32_OperatingSystem")
>
> Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
>
> Dim os As ManagementObject
>
> Try
>
> scope.Connect()
>
> Catch ex As Exception
>
> WOULD LIKE TO EXIT LOOP HERE AND GO ON TO NEXT NUMBER
>
> End Try
>
> For Each os In searcher.Get()
>
> osname = os("caption")
>
> dirname = os("windowsdirectory")
>
> servername = os("name")
>
>
>
> End If
>
>
>
> Next os
>
>



Relevant Pages

  • exiting a for loop.
    ... I test the number and exception is thrown if there is an error. ... How do I get the program to stop in the middle of the loop and go to the ... Dim searcher As New ManagementObjectSearcher ...
    (microsoft.public.dotnet.languages.vb)
  • Exiting a for loop
    ... I test the number and exception is thrown if there is an error. ... How do I get the program to stop in the middle of the loop and go to the ... Dim searcher As New ManagementObjectSearcher ...
    (microsoft.public.dotnet.framework)
  • Re: long double versions of functions in gcc under Cygwin
    ... rather than the nearest enclosing one) and a decent exception ... them it doesn't seem like goto usage would be affected ... int typfun() ... Why use a for loop when it is just a while loop in disguise? ...
    (comp.lang.c)
  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ... If you use Sleepin a loop, your design is probably wrong and needs to be ... The "First Chance Exception" message usually indicates nothing harmful. ...
    (microsoft.public.vc.mfc)
  • Re: Get_Line problem (GNAT bug?)
    ... Now, if the program specs says: "read the lines from input until EOF", then this for me immediately translates into a loop with some exit condition. ... "Read until" - you have a regular end-of-sequence condition here. ... I'm not convinced that exception might be a correct design choice for breaking the loop that reads data from well formatted file. ...
    (comp.lang.ada)