2

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

Gwen Wong
  • 357
  • 3
  • 15

5 Answers5

0

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.

skiwi
  • 66,971
  • 31
  • 131
  • 216
  • 1
    That's basically OP's first example, did you notice it? – Marko Topolnik Mar 01 '14 at 12:51
  • Think he meant that its neater then if() {} else {} :D I wonder-- would this then be possible in while loops then? – Gwen Wong Mar 01 '14 at 12:52
  • @MarkoTopolnik I see a fundamental difference. What I believe the OP was thinking to do is to wrap everything around in `if-else`-statements, leading to tremendous code clutter with more conditions. What I propose is to move the boolean check forward, before the interesting stuff starts to happen. – skiwi Mar 01 '14 at 12:52
  • @GwenWong I do not see a way to do it nicely. – skiwi Mar 01 '14 at 12:54
  • I grant that this might be one (smaller) aspect of what bothers OP in his (her?) code. – Marko Topolnik Mar 01 '14 at 12:55
0

If you just want the "if" block out of the for loop, then you can do it the old-fasioned way.

Iterator<Character[]> i=charSets.iterator();
for(Character[] charArr=i.next(); i.hasNext() && doesWordMatch == true; charArr=i.next() ) {
        // do something
}
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • This snippet is not robust to an empty collection, but I agree with the suggestion of using an iterator and a standard for-loop if the OP wants to inline an extra condition into their for-loop check. – avik Mar 01 '14 at 13:14
0

If what you want is to create a mapping between charArr and doesWordMatch:

Map<Character[], Boolean> map = new HashMap<>();
map.put(charArr, doesWordMatch);

for(Map.Entry<Character[], Boolean> myEntry : map.entrySet()) {
    if(myEntry.getValue()) {
        // do something with myEntry.getKey();
    } else {
        break;
    }
}

I'm not sure if that's what you're looking for, but I can't think of any other reason for wanting the doesWordMatch variable into the loop. In this case, doesWordMatch can be different for each charArr.

grdryn
  • 1,972
  • 4
  • 19
  • 28
0

Use an iterator and a standard for-loop with a blank increment statement:

Character[] curr;
for(Iterator<Character[]> iter = charSets.iterator(); iter.hasNext() && doesWordMatch;) {
  curr = iter.next();
  // ...
}
avik
  • 2,708
  • 17
  • 20
-2

Will the boolean variable doesWordMatch change in the for-loop?

If not, I would suggest:

if(doesWordMatch)
  for(Character[] charArr : charSets)
  {
    //dosomething
  }
}
Gosta J
  • 86
  • 5
  • 2
    I strongly believe that the `doesWordMatch` changes during the iteration, else imho this whole question would make no sense. – skiwi Mar 01 '14 at 12:59
  • I wasn't asking you, I was asking the OP. If you are correct your suggestion above isn't that nice, why enter the for loop if you know that you shouldn't? – Gosta J Mar 01 '14 at 13:20
  • @skiwi And why are you downvoting a suggestion better than yours just because I have a question in it that you think you have the answer to? – Gosta J Mar 01 '14 at 13:50
  • I have not downvoted you, please keep your comments for yourself. – skiwi Mar 01 '14 at 14:34