Search code examples
javaloopsfor-loopforeachtraversal

add additional parameters to foreach loop


I have a foreach loop that goes

boolean doesWordMatch = false;
for(Character[] charArr : charSets)
{
  if(doesWordMatch)
    //dosomething
  else
    break;
}

I was wondering, is there anyway to put the condition into the for loop? e.g.

for(Character[] charArr : charSets && doesWordMatch == true)
{
  //dosomething
}

edit-- Right, so would this be possible in a while loop? :o


Solution

  • No, this cannot be done in an enhanced for-loop.

    You could try the following:

    for (Character[] charArr : charSets) {
        if (!doesWordMatch) {
            break;
        }
        //do interesting things
    }
    

    I believe this is also more concise than having everything in the looping declaration.