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

adding a condition to the enhanced for loop

Hello,

I was wondering if it's possible to add a condition to the enhanced for
loop. This is an example of an enhanced for loop:

LinkedList<MyClass> children = new LinkedList<MyClass>();
boolean found = false;

children.add(new MyClass("Extended"));
children.add(new MyClass("Enhanced"));
children.add(new MyClass("Embraced"));

for (MyClass c: children) {
if (c.getName().equals("Enhanced") {
found = true;
}
}

This loop would continue right to the end though, even if "Enhanced" was
found at the first element. I would like to build an extra condition into
the loop, stating that it must continue but only while !found (this means
found == false).

Any ideas how to do this? Or is it not possible with an enhanced for loop?
Should I use a normal loop, and iterate through the elements using an
Iterator?

Kind regards,

--
Jo Vermeulen
Student Computer Science at the tUL
email: jo@lumumba.luc.ac.be
www: http://lumumba.luc.ac.be/jo
Jul 17 '05 #1
2 6895
Jo Vermeulen wrote:
Hello,

I was wondering if it's possible to add a condition to the enhanced for
loop. This is an example of an enhanced for loop:

LinkedList<MyClass> children = new LinkedList<MyClass>();
boolean found = false;

children.add(new MyClass("Extended"));
children.add(new MyClass("Enhanced"));
children.add(new MyClass("Embraced"));

for (MyClass c: children) {
if (c.getName().equals("Enhanced") {
found = true;
}
}

This loop would continue right to the end though, even if "Enhanced" was
found at the first element. I would like to build an extra condition into
the loop, stating that it must continue but only while !found (this means
found == false).

Any ideas how to do this? Or is it not possible with an enhanced for loop?
Should I use a normal loop, and iterate through the elements using an
Iterator?


I don't think you can add a condition directly to the for loop, but why
not try a break statement?
for (MyClass c: children)
{
// blah blah blah

if (done())
{
break;
}
}
Jul 17 '05 #2
Op Mon, 31 May 2004 13:54:16 +0000, schreef Raymond DeCampo:
I don't think you can add a condition directly to the for loop, but why
not try a break statement?
for (MyClass c: children)
{
// blah blah blah

if (done())
{
break;
}
}


Hmm, haven't thought of it.. That could indeed do the trick. Hope it's
good coding style though :-)

Kind regards,

--
Jo Vermeulen
Student Computer Science at the tUL
email: jo@lumumba.luc.ac.be
www: http://lumumba.luc.ac.be/jo
Jul 17 '05 #3

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

Similar topics

24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
36
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at...
2
by: YFS DBA | last post by:
Hello again; I've got a small invoicing database I'm trying to create. One of the tables is set up as follows: Client# Date Claim# Amount 1001 10/10/03 ...
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) {
3
by: Douglas | last post by:
Hi, In the loop for(i=0; i< h+1; i++); if h=10 say, then is h+1 evaluated every time or, between iterations, does the for loop remember that the condition i<11 is being applied? I'm guessing...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
2
by: dotnetnewbie | last post by:
Whilst looping through items in a listview I wish to have the option of inserting a new row (in the middle not necessarily at the end of the listview). thus if lvw is the listview name dim...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
1
by: jholg | last post by:
Hi, regarding automatically adding functionality to a class (basically taken from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code: #-----------------...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.