Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Exit For in C#?

Exit For in C#?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hendrik Debedts
    wrote on last edited by
    #1

    What's the 'Exit For' equivalent in C#?

    P 1 Reply Last reply
    0
    • H Hendrik Debedts

      What's the 'Exit For' equivalent in C#?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      break ?

      H 1 Reply Last reply
      0
      • P PIEBALDconsult

        break ?

        H Offline
        H Offline
        Hendrik Debedts
        wrote on last edited by
        #3

        thx

        P 1 Reply Last reply
        0
        • H Hendrik Debedts

          thx

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Most times when you use this it indicates that you have used the wrong construct. If you are issuing a break then this shows that you have reached a terminating condition. If you need to drop out of a loop early, this would indicate that you should be using something like a while loop instead. See the following:

          for (int i = 0; i < 10 ; i++)
          {
            // Do something
            if (i == 2) break;
          }
          

          Contrast this with:

          int i = 0;
          
          while (i++ != 2)
          {
           // Do something
          }
          

          the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
          Deja View - the feeling that you've seen this post before.

          L A 2 Replies Last reply
          0
          • P Pete OHanlon

            Most times when you use this it indicates that you have used the wrong construct. If you are issuing a break then this shows that you have reached a terminating condition. If you need to drop out of a loop early, this would indicate that you should be using something like a while loop instead. See the following:

            for (int i = 0; i < 10 ; i++)
            {
              // Do something
              if (i == 2) break;
            }
            

            Contrast this with:

            int i = 0;
            
            while (i++ != 2)
            {
             // Do something
            }
            

            the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
            Deja View - the feeling that you've seen this post before.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            If you need to drop out of a loop early, this would indicate that you should be using something like a while loop instead.

            Or a different for expression

            Node n = null;
            NodeList list = doc.selectNodes("*");
            for(int j=0, null == n && j < list.Length, j++)
            {
            if( ???)
            n = list[j];
            }

            led mike

            1 Reply Last reply
            0
            • P Pete OHanlon

              Most times when you use this it indicates that you have used the wrong construct. If you are issuing a break then this shows that you have reached a terminating condition. If you need to drop out of a loop early, this would indicate that you should be using something like a while loop instead. See the following:

              for (int i = 0; i < 10 ; i++)
              {
                // Do something
                if (i == 2) break;
              }
              

              Contrast this with:

              int i = 0;
              
              while (i++ != 2)
              {
               // Do something
              }
              

              the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
              Deja View - the feeling that you've seen this post before.

              A Offline
              A Offline
              Alexandru Lungu
              wrote on last edited by
              #6

              Ya, Ya, we all know the theory; but break is more handier

              P P 2 Replies Last reply
              0
              • A Alexandru Lungu

                Ya, Ya, we all know the theory; but break is more handier

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Handy doesn't make it right. Just lazy. If you take shortcuts here, where else do you take shortcuts? Whenever I see something like this, it sets the alarm bells ringing that there are problems with the design.

                the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • A Alexandru Lungu

                  Ya, Ya, we all know the theory; but break is more handier

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  And if it's due to an exceptional state, it may be more expressive. But certainly use it with caution.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups