adding a condition to the enhanced for loop

From: Jo Vermeulen (jo_at_lumumba.luc.ac.be)
Date: 05/31/04


Date: Mon, 31 May 2004 11:40:28 +0200

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


Relevant Pages

  • Re: Simple newbie question
    ... Duncan Booth wrote: ... > The loop will assign the first element of lista to line, ... > The list comprehension rebinds the lista variable, ...
    (comp.lang.python)
  • Re: Finite $_POST loops infinitely - help!
    ... No matter what I put within the foreach loop, the loop ran infinitely, ... It constantly read $key as the very first element in $array, in short, ... What I suspect at this point that this is a PHP 4.3+ bug. ...
    (comp.lang.php)
  • Re: A way to re-organize a list
    ... and then by the first element. ... The inner loop is trying to make multi-level grouping. ...
    (comp.lang.python)
  • Re: Optimisation code check
    ... optimising. ... first element in each row is below a predefined value. ... Then the inside of your loop would simplify to ... "Any sufficiently advanced bug is indistinguishable ...
    (comp.soft-sys.matlab)
  • Re: problem in using "fopen" inside a loop
    ... I have a file, 'list2.txt', which contains all my filenames that need ... By using the following loop I read the filenames ... Exercise your debugging skills: ... How do you access the first element as a ...
    (comp.soft-sys.matlab)