exit loop iteration case

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    #1

    exit loop iteration case


    Hello,

    I am iterating through objects and have the following case:

    foreach (classA objectA in classAList)
    {
    if (classA.count0)
    {
    //now I need to get out of the foreach loop if so,otherwise to keep
    iterating
    }

    }

    What is the best way to do it?

    Thanks!

    *** Sent via Developersdex http://www.developersdex.com ***
  • Navid

    #2
    Re: exit loop iteration case

    csharpula csharp wrote:
    Hello,
    >
    I am iterating through objects and have the following case:
    >
    foreach (classA objectA in classAList)
    {
    if (classA.count0)
    {
    //now I need to get out of the foreach loop if so,otherwise to keep
    iterating
    }
    >
    }
    >
    What is the best way to do it?
    >
    Thanks!
    >
    *** Sent via Developersdex http://www.developersdex.com ***
    to simply just get out of the iteration is some condition becomes true
    you can use break;


    foreach (classA objectA in classAList)
    {
    if (classA.count0)
    {
    break;
    //now I need to get out of the foreach loop if so,otherwise to
    keeiterating
    }
    }

    Comment

    • Jeff Johnson

      #3
      Re: exit loop iteration case

      "csharpula csharp" <csharpula@yaho o.comwrote in message
      news:u1Oap8SNJH A.2044@TK2MSFTN GP04.phx.gbl...
      I am iterating through objects and have the following case:
      >
      foreach (classA objectA in classAList)
      {
      if (classA.count0)
      {
      //now I need to get out of the foreach loop if so,otherwise to keep
      iterating
      }
      >
      }
      >
      What is the best way to do it?
      You've been given the answer, but just for completion, the opposite (so to
      speak) of "break" is "continue." It's used far less and some would probably
      argue that if you structure your code right you'll never need to use it.


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: exit loop iteration case

        On Oct 23, 7:35 pm, "Jeff Johnson" <i....@enough.s pamwrote:
        You've been given the answer, but just for completion, the opposite (so to
        speak) of "break" is "continue." It's used far less and some would probably
        argue that if you structure your code right you'll never need to use it.
        Those are probably the same people who always use a single exit point
        from a method, regardless of how much less readable that makes the
        code ;)

        Jon

        Comment

        • Peter Morris

          #5
          Re: exit loop iteration case

          >
          Those are probably the same people who always use a single exit point
          from a method, regardless of how much less readable that makes the
          code ;)
          <

          Despite people I know advocating having only a single exit point from every
          method, I am always much happier doing this

          public void DoSomethingIfNo tNull(object instance)
          {
          if (instance == null)
          return;

          //lots of code here
          }

          than this

          public void DoSomethingIfNo tNull(object instance)
          {
          if (instance != null)
          {
          //lots of code here
          }
          }



          --
          Pete
          ====



          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: exit loop iteration case

            On Oct 24, 10:11 am, "Peter Morris" <mrpmorri...@SP AMgmail.comwrot e:
            > Those are probably the same people who always use a single exit point
            from a method, regardless of how much less readable that makes the
            code ;)
            <
            >
            Despite people I know advocating having only a single exit point from every
            method, I am always much happier doing this
            <snip>

            Exactly. It's just a case of being pragmatic.

            Jon

            Comment

            • Jeff Johnson

              #7
              Re: exit loop iteration case

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:45897494-3173-4226-a1b0-244b6f6f6b82@c6 0g2000hsf.googl egroups.com...
              Exactly. It's just a case of being pragmatic.
              <AOL>
              Me too!
              </AOL>


              Comment

              • Peter Morris

                #8
                Re: exit loop iteration case

                Exactly. It's just a case of being pragmatic.


                I tried it once;
                but hated seeing;
                text that looked
                like this; // :-)
                }
                }
                }
                }
                }
                }

                Comment

                Working...