473,396 Members | 1,996 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,396 software developers and data experts.

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 ***
Oct 23 '08 #1
7 2707
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
}
}
Oct 23 '08 #2
"csharpula csharp" <cs*******@yahoo.comwrote in message
news:u1**************@TK2MSFTNGP04.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.
Oct 23 '08 #3
On Oct 23, 7:35*pm, "Jeff Johnson" <i....@enough.spamwrote:
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
Oct 24 '08 #4
>
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 DoSomethingIfNotNull(object instance)
{
if (instance == null)
return;

//lots of code here
}

than this

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

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 24 '08 #5
On Oct 24, 10:11*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
> 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
Oct 24 '08 #6
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:45**********************************@c60g2000 hsf.googlegroups.com...
Exactly. It's just a case of being pragmatic.
<AOL>
Me too!
</AOL>
Oct 24 '08 #7
Exactly. It's just a case of being pragmatic.
I tried it once;
but hated seeing;
text that looked
like this; // :-)
}
}
}
}
}
}
Oct 24 '08 #8

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

Similar topics

6
by: Shill | last post by:
I have several questions. In C, AFAIU, a for loop is just syntactic sugar for a while loop. for (i1; i2; i3) i4; is equivalent to i1 while (i2) {
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
8
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}''...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
8
by: Jim Langston | last post by:
There's the thing about iterating though a map or vector when you may delete one of the elements, where you simply assign the iterator to the map.erase() statement or increment it if you don't. ...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.