Break out of a loop
-
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: ^_^
Programm3r wrote:
if (globalFileName == compareString.Substring(1, (compareString.Length - 1))) { /* File was found */ return file; }
if you mean this bit, it already does by virtue of the
return
keyword. In general you can break a look by using thebreak
keyword. For completeness you may also like to look up thecontinue
keyword in c# -
AFAIK the only way to break out of foreach look is with a "break" statement. There are some interesting discussions, and yes blogs, around the net about foreach versus for loops in .NET. You might want to check them out.
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: ^_^
-
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: ^_^
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;
} -
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: ^_^
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
-
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
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
-
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
-
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
if (usingGoto) { goto jailAndDoNotPassGo; }
Deja View - the feeling that you've seen this post before.
-
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
Other than in a C# switch?
-
Other than in a C# switch?
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
-
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
-
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
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
-
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: ^_^
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"?