Exiting nested For loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    Exiting nested For loops

    I have several nested For loops, as follows:

    For a As Integer = 0 To 255
    For b As Integer = 0 To 255
    For c As Integer = 0 To 255
    If <Boolean ExpressionThen <My CodeElse Exit For
    Next
    If Not <Boolean ExpressionThen Exit For
    Next
    If Not <Boolean ExpressionThen Exit For
    Next

    As you can see, I need to test the condition in each For, which in my case
    is probably more costly than necessary. If I could simply say exit all For
    loops or exit 3 For loops or something, not only would it be less costly,
    but simpler code. Is there any way to do this (or maybe a nice workaround)?
    Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Hal Rosser

    #2
    Re: Exiting nested For loops

    cut-n-pasted from the help files...Looks like you would need to nest a for
    loop in a while loop and would then be limited to exiting from only one
    nesting.

    Skipping from Within a Nested Loop. If you have Do, For, or While loops
    nested one within another, you can skip immediately to the next iteration of
    any level in the nesting. This is only true, however, when the loops are of
    different types. If you have nested loops of the same type, for example
    nested While loops, Continue While skips to the next iteration of the
    innermost While loop.
    To skip to the next iteration of a Do loop from within a nested For loop
    1.. Write the nested loops in the normal way.

    2.. Use Continue Do at any place that you want to terminate the current
    iteration of the inner For loop and skip to the next iteration of the outer
    Do loop.

    Copy Code
    Public Sub divideElements( ByRef matrix(,) As Double)
    Dim i As Integer = -1
    Do Until i matrix.GetUpper Bound(0)
    i += 1
    For j As Integer = 0 To matrix.GetUpper Bound(1)
    If matrix(j, j) = 0 Then Continue Do
    matrix(i, j) /= matrix(j, j)
    Next j
    Loop
    End Sub

    "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
    news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
    >I have several nested For loops, as follows:
    >
    For a As Integer = 0 To 255
    For b As Integer = 0 To 255
    For c As Integer = 0 To 255
    If <Boolean ExpressionThen <My CodeElse Exit For
    Next
    If Not <Boolean ExpressionThen Exit For
    Next
    If Not <Boolean ExpressionThen Exit For
    Next
    >
    As you can see, I need to test the condition in each For, which in my case
    is probably more costly than necessary. If I could simply say exit all For
    loops or exit 3 For loops or something, not only would it be less costly,
    but simpler code. Is there any way to do this (or maybe a nice
    workaround)?
    Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

    >
    >



    Comment

    • Stephany Young

      #3
      Re: Exiting nested For loops

      Try
      For a As Integer = 0 To 255
      For b As Integer = 0 To 255
      For c As Integer = 0 To 255
      If Not <Boolean ExpressionThen Exit Try
      <My Code>
      Next
      Next
      Next
      Catch
      End Try

      "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
      news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
      >I have several nested For loops, as follows:
      >
      For a As Integer = 0 To 255
      For b As Integer = 0 To 255
      For c As Integer = 0 To 255
      If <Boolean ExpressionThen <My CodeElse Exit For
      Next
      If Not <Boolean ExpressionThen Exit For
      Next
      If Not <Boolean ExpressionThen Exit For
      Next
      >
      As you can see, I need to test the condition in each For, which in my case
      is probably more costly than necessary. If I could simply say exit all For
      loops or exit 3 For loops or something, not only would it be less costly,
      but simpler code. Is there any way to do this (or maybe a nice
      workaround)? Thanks.
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

      >

      Comment

      • Stanimir Stoyanov

        #4
        Re: Exiting nested For loops

        Hello Nathan,

        I personally use labels and GoTo statements in such cases. Of course they
        names are customizable.


        For a As Integer = 0 To 255
        For b As Integer = 0 To 255
        For c As Integer = 0 To 255
        If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
        Next
        SkipOne:
        If Not <Boolean ExpressionThen GoTo SkipTwo
        Next
        SkipTwo:
        If Not <Boolean ExpressionThen GoTo SkipThree
        Next

        SkipThree:
        ' Continue with your code here

        --
        Stanimir Stoyanov


        "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
        news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
        >I have several nested For loops, as follows:
        >
        For a As Integer = 0 To 255
        For b As Integer = 0 To 255
        For c As Integer = 0 To 255
        If <Boolean ExpressionThen <My CodeElse Exit For
        Next
        If Not <Boolean ExpressionThen Exit For
        Next
        If Not <Boolean ExpressionThen Exit For
        Next
        >
        As you can see, I need to test the condition in each For, which in my case
        is probably more costly than necessary. If I could simply say exit all For
        loops or exit 3 For loops or something, not only would it be less costly,
        but simpler code. Is there any way to do this (or maybe a nice
        workaround)? Thanks.
        --
        Nathan Sokalski
        njsokalski@hotm ail.com
        有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

        >

        Comment

        • Stanimir Stoyanov

          #5
          Re: Exiting nested For loops

          Hello Nathan,

          I personally use labels and GoTo statements in such cases. Of course they
          names are customizable.


          For a As Integer = 0 To 255
          For b As Integer = 0 To 255
          For c As Integer = 0 To 255
          If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
          Next
          SkipOne:
          If Not <Boolean ExpressionThen GoTo SkipTwo
          Next
          SkipTwo:
          If Not <Boolean ExpressionThen GoTo SkipThree
          Next

          SkipThree:
          ' Continue with your code here

          --
          Stanimir Stoyanov


          "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
          news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
          >I have several nested For loops, as follows:
          >
          For a As Integer = 0 To 255
          For b As Integer = 0 To 255
          For c As Integer = 0 To 255
          If <Boolean ExpressionThen <My CodeElse Exit For
          Next
          If Not <Boolean ExpressionThen Exit For
          Next
          If Not <Boolean ExpressionThen Exit For
          Next
          >
          As you can see, I need to test the condition in each For, which in my case
          is probably more costly than necessary. If I could simply say exit all For
          loops or exit 3 For loops or something, not only would it be less costly,
          but simpler code. Is there any way to do this (or maybe a nice
          workaround)? Thanks.
          --
          Nathan Sokalski
          njsokalski@hotm ail.com
          有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

          >

          Comment

          • Stanimir Stoyanov

            #6
            Re: Exiting nested For loops

            Hello Nathan,

            I personally use labels and GoTo statements in such cases. Of course they
            names are customizable.


            For a As Integer = 0 To 255
            For b As Integer = 0 To 255
            For c As Integer = 0 To 255
            If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
            Next
            SkipOne:
            If Not <Boolean ExpressionThen GoTo SkipTwo
            Next
            SkipTwo:
            If Not <Boolean ExpressionThen GoTo SkipThree
            Next

            SkipThree:
            ' Continue with your code here

            --
            Stanimir Stoyanov


            "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
            news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
            >I have several nested For loops, as follows:
            >
            For a As Integer = 0 To 255
            For b As Integer = 0 To 255
            For c As Integer = 0 To 255
            If <Boolean ExpressionThen <My CodeElse Exit For
            Next
            If Not <Boolean ExpressionThen Exit For
            Next
            If Not <Boolean ExpressionThen Exit For
            Next
            >
            As you can see, I need to test the condition in each For, which in my case
            is probably more costly than necessary. If I could simply say exit all For
            loops or exit 3 For loops or something, not only would it be less costly,
            but simpler code. Is there any way to do this (or maybe a nice
            workaround)? Thanks.
            --
            Nathan Sokalski
            njsokalski@hotm ail.com
            有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

            >

            Comment

            • Stanimir Stoyanov

              #7
              Re: Exiting nested For loops

              Hello Nathan,

              I personally use labels and GoTo statements in such cases. Of course they
              names are customizable.


              For a As Integer = 0 To 255
              For b As Integer = 0 To 255
              For c As Integer = 0 To 255
              If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
              Next
              SkipOne:
              If Not <Boolean ExpressionThen GoTo SkipTwo
              Next
              SkipTwo:
              If Not <Boolean ExpressionThen GoTo SkipThree
              Next

              SkipThree:
              ' Continue with your code here

              --
              Stanimir Stoyanov


              "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
              news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
              >I have several nested For loops, as follows:
              >
              For a As Integer = 0 To 255
              For b As Integer = 0 To 255
              For c As Integer = 0 To 255
              If <Boolean ExpressionThen <My CodeElse Exit For
              Next
              If Not <Boolean ExpressionThen Exit For
              Next
              If Not <Boolean ExpressionThen Exit For
              Next
              >
              As you can see, I need to test the condition in each For, which in my case
              is probably more costly than necessary. If I could simply say exit all For
              loops or exit 3 For loops or something, not only would it be less costly,
              but simpler code. Is there any way to do this (or maybe a nice
              workaround)? Thanks.
              --
              Nathan Sokalski
              njsokalski@hotm ail.com
              有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

              >

              Comment

              • Cor Ligthert[MVP]

                #8
                Re: Exiting nested For loops

                Nathan,

                Why are you using the second and thirth if, in the way you tell it, it
                should never has to be reached?

                Cor


                "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
                news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
                >I have several nested For loops, as follows:
                >
                For a As Integer = 0 To 255
                For b As Integer = 0 To 255
                For c As Integer = 0 To 255
                If <Boolean ExpressionThen <My CodeElse Exit For
                Next
                If Not <Boolean ExpressionThen Exit For
                Next
                If Not <Boolean ExpressionThen Exit For
                Next
                >
                As you can see, I need to test the condition in each For, which in my case
                is probably more costly than necessary. If I could simply say exit all For
                loops or exit 3 For loops or something, not only would it be less costly,
                but simpler code. Is there any way to do this (or maybe a nice
                workaround)? Thanks.
                --
                Nathan Sokalski
                njsokalski@hotm ail.com
                有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

                >

                Comment

                • Family Tree Mike

                  #9
                  Re: Exiting nested For loops

                  I think this is more understandable to the next person to maintain your
                  code:

                  a = 0
                  While (a < 256 And Not quit)
                  b = 0
                  While (b < 256 And Not quit)
                  c = 0
                  While (c < 256 And Not quit)

                  If (anotherbool) Then
                  quit = True
                  End If

                  c = c + 1
                  End While
                  b = b + 1
                  End While
                  a = a + 1
                  End While


                  "Nathan Sokalski" <njsokalski@hot mail.comwrote in message
                  news:er86xI9OJH A.5080@TK2MSFTN GP03.phx.gbl...
                  >I have several nested For loops, as follows:
                  >
                  For a As Integer = 0 To 255
                  For b As Integer = 0 To 255
                  For c As Integer = 0 To 255
                  If <Boolean ExpressionThen <My CodeElse Exit For
                  Next
                  If Not <Boolean ExpressionThen Exit For
                  Next
                  If Not <Boolean ExpressionThen Exit For
                  Next
                  >
                  As you can see, I need to test the condition in each For, which in my case
                  is probably more costly than necessary. If I could simply say exit all For
                  loops or exit 3 For loops or something, not only would it be less costly,
                  but simpler code. Is there any way to do this (or maybe a nice
                  workaround)? Thanks.
                  --
                  Nathan Sokalski
                  njsokalski@hotm ail.com
                  有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

                  >

                  Comment

                  Working...