473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How TO Exit a foreach early

Tom
I know how to construct a foreach.

I need to know if a condition is met, how do I exit the
foreach early so as avaoid the roundtrips for the loop.

Thanks

Tom
Nov 15 '05 #1
5 90001
break;

"Tom" <da*****@fhlbcin.com> wrote in message
news:0a****************************@phx.gbl...
I know how to construct a foreach.

I need to know if a condition is met, how do I exit the
foreach early so as avaoid the roundtrips for the loop.

Thanks

Tom

Nov 15 '05 #2
Tom <da*****@fhlbcin.com> wrote:
I know how to construct a foreach.

I need to know if a condition is met, how do I exit the
foreach early so as avaoid the roundtrips for the loop.


You can use break and continue in foreach just as you can in for:

using System;

public class Test
{
static void Main()
{
string[] foo = new string[] {"first", "second", "third"};

foreach (string x in foo)
{
Console.WriteLine(x);
if (x=="second")
{
Console.WriteLine ("Exiting loop");
break;
}
}
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
use break;
foreach()
{
//found
break;
}
"Tom" <da*****@fhlbcin.com> wrote in message
news:0a****************************@phx.gbl...
I know how to construct a foreach.

I need to know if a condition is met, how do I exit the
foreach early so as avaoid the roundtrips for the loop.

Thanks

Tom

Nov 15 '05 #4
Maybe "while" will be better ?

Gawel
Nov 15 '05 #5
Gawelek <ga*****@NOSPAMEKpoczta.gazeta.pl> wrote:
Maybe "while" will be better ?


Why? You've then got to do the iteration separately. I can see that in
some cases that would provide clearer code, but I don't think it would
most of the time. Then again, I know there's considerable debate about
the pros and cons of break, continue, and having multiple return points
in a method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: lallous | last post by:
Hello, This question was asked in comp.lang.c++ and the answers involved the use of objects whose destructors are automatically called when getting out of scope, however I was expecting...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
6
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI...
2
by: Learning.Net | last post by:
hi , I have a application which reads files, directory,and its version and version information is written to text file.Its working fine if files in directory are less but problem arises when no...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
8
by: =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post by:
An example of a slightly more complicated class might be to have a collection of first names, and a collection of last names in your class. The IEnumerable functions then could return the complete...
8
by: Bill Butler | last post by:
"raylopez99" <raylopez99@yahoo.comwrote in message news:bd59f62a-5b54-49e8-9872-ed9aef676049@t54g2000hsg.googlegroups.com... <snip> I don't think "right" is the correct word. There are many...
2
by: paul138 | last post by:
Hi, I have created an index in Excel that goes up by 1 everytime a macro is run. (cell c99 in Sheet1) I.e. it starts at one, i run the macro and it now shows 2. I wish to add some code to my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.