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. C#
  4. Break out of a loop

Break out of a loop

Scheduled Pinned Locked Moved C#
comtoolshelpquestion
14 Posts 9 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.
  • P Programm3r

    Hi Mike, Thanks for your reply and comments but, break does not work. I have tried it without success. Regards,


    The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

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

    Programm3r wrote:

    Thanks for your reply and comments but, break does not work. I have tried it without success.

    works for me

    string[] test = {"help","me","with","for","loops"};
    foreach(string t in test)
    {
    Console.WriteLine(t);
    if( t.Equals("for"))
    break;
    }

    1 Reply Last reply
    0
    • P Programm3r

      Hi all, I would like to know how I can get out of this loop the first time the condition is true? public string DirSearch(string sDir) { try { string compareString = ""; foreach ( string dir in Directory.GetDirectories(sDir)) { foreach ( string file in Directory.GetFiles(dir)) { compareString = file.Substring(file.LastIndexOf("\\"),(file.Length-file.LastIndexOf("\\"))); if (globalFileName == compareString.Substring(1, (compareString.Length - 1))) { /* File was found */ return file; } } DirSearch(dir); } return ""; } catch (System.Exception excpt) { MessageBox.Show(excpt.Message,"Exception Occured",MessageBoxButtons.OK,MessageBoxIcon.Error); return ""; } }
      Many Thanks Regards,


      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

      S Offline
      S Offline
      Shpendh
      wrote on last edited by
      #6

      you can use also "goto" short example: string filename = ""; for(int i = 0; i < 10; i++) { if(i == 3) { filename = "path" goto FileHasFounded; } } FileHasFounded: MessageBox.Show("file name is this: " filename); break don't work in this case because you have two loops. and it will break only from one loop hope it will help. respect.

      spaps

      C 1 Reply Last reply
      0
      • S Shpendh

        you can use also "goto" short example: string filename = ""; for(int i = 0; i < 10; i++) { if(i == 3) { filename = "path" goto FileHasFounded; } } FileHasFounded: MessageBox.Show("file name is this: " filename); break don't work in this case because you have two loops. and it will break only from one loop hope it will help. respect.

        spaps

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #7

        Shpendh wrote:

        you can use also "goto"

        The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.


        Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

        J P P S 4 Replies Last reply
        0
        • C Colin Angus Mackay

          Shpendh wrote:

          you can use also "goto"

          The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.


          Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #8

          Exactly what I was thinking. Number of times ive used goto in c#: zero!

          M 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Shpendh wrote:

            you can use also "goto"

            The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.


            Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

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

            if (usingGoto) { goto jailAndDoNotPassGo; }

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              Shpendh wrote:

              you can use also "goto"

              The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.


              Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

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

              Other than in a C# switch?

              C 1 Reply Last reply
              0
              • P PIEBALDconsult

                Other than in a C# switch?

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #11

                PIEBALDconsult wrote:

                Other than in a C# switch?

                Nope. I don't use gotos in switch statements - although I am aware that it is one of the few valid places you might consider using them. In fact I don't use switch statements all that often as I generally (but not always) consider them a sign of a poor design. Although I think that is just because I've seen them used badly in so many situations.


                Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

                1 Reply Last reply
                0
                • J J4amieC

                  Exactly what I was thinking. Number of times ive used goto in c#: zero!

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #12

                  Breaking out of a couple of nested loops? (I always wanted to be able to name loops, or do a break(2); or something similar)... You're right though - theres very very few usages of goto that are "legitimate".

                  Mark Churchill Director Dunn & Churchill

                  1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    Shpendh wrote:

                    you can use also "goto"

                    The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.


                    Upcoming FREE developer events: * Developer! Developer! Developer! 6 * Developer Day Scotland My website

                    S Offline
                    S Offline
                    Shpendh
                    wrote on last edited by
                    #13

                    i didn't say that "goto" is the only one choise in this solution, i wanted to help him with one of the solution, i never didn't use "goto" in my application but i have only learned about it. but anyway sorry, maybe this example will help, string filename = ""; bool isFound = false; foreach(string str in strColl) { if(!isFound) { foreach(string str2 in strColl2) { if(str2 == "founded") { filename = str2; isFound = true; break;//break from first loop } } } else { break;//break from second loop } } hope this will help,

                    spaps

                    1 Reply Last reply
                    0
                    • P Programm3r

                      Hi all, I would like to know how I can get out of this loop the first time the condition is true? public string DirSearch(string sDir) { try { string compareString = ""; foreach ( string dir in Directory.GetDirectories(sDir)) { foreach ( string file in Directory.GetFiles(dir)) { compareString = file.Substring(file.LastIndexOf("\\"),(file.Length-file.LastIndexOf("\\"))); if (globalFileName == compareString.Substring(1, (compareString.Length - 1))) { /* File was found */ return file; } } DirSearch(dir); } return ""; } catch (System.Exception excpt) { MessageBox.Show(excpt.Message,"Exception Occured",MessageBoxButtons.OK,MessageBoxIcon.Error); return ""; } }
                      Many Thanks Regards,


                      The only programmers that are better that C programmers are those who code in 1's and 0's :bob: :)Programm3r My Blog: ^_^

                      A Offline
                      A Offline
                      Alan N
                      wrote on last edited by
                      #14

                      According to the C# 2.0 standard ISO/IEC 23270 15.9.1 The break statement .... .... When multiple switch, while, do, for, or foreach statements are nested within each other, a break statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement (§15.9.3) shall be used. So there you are, it's official, use a goto. Is there a smiley for "light the blue touchpaper and stand well back"?

                      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