6

This question comes from my OCD-ish nature. Say i have this piece of code.

boolean executed = false;
for(Object o : Collection){
    if((o fulfills condition) && executed  == false){
        //do something
        executed  = true;
    } 
    //other code
}

If the specification only requires that the if statement executes once, is there a better way to skip checking the if conditional than setting executed to true? It bothers me that the loop needs to check the if conditional every single loop iteration, after the if statement has already been executed.

The point of the executed boolean is to prevent the if statement from executing again. I'm not sure this is possible, but i want to take it one step further, and skip checking the if conditional once executed is true.

EDIT: I still need to finish the loop after the condition is met.

Eric Guan
  • 15,474
  • 8
  • 50
  • 61
  • What happens once ```executed``` becomes true? – RandomQuestion Aug 26 '15 at 23:12
  • possible duplicate of [What is the "continue" keyword and how does it work in Java?](http://stackoverflow.com/questions/389741/what-is-the-continue-keyword-and-how-does-it-work-in-java) – Kick Buttowski Aug 26 '15 at 23:13
  • Do you want to apply some operation (which makes that element fulfill condition) on all elements only once? – RandomQuestion Aug 26 '15 at 23:13
  • 1
    `continue;` will start the next iteration, not sure if this is what you're looking for. – Kevin Aug 26 '15 at 23:15
  • By reaching the continue keyword, the program will have had to check the if conditional. I want to avoid checking the if conditional. I'm aware of break and continue, they're not relevant to my question really. – Eric Guan Aug 26 '15 at 23:17

5 Answers5

7

Not really; you're stuck checking that conditional each time with this code flow. There are ways to make it less expensive, though.

I would advise against using continue, since it will cause you to check the conditional repeatedly and then fall through, which probably isn't what you want.

The simplest thing to do may be to reorder the boolean AND statement.

boolean executed = false;
for(Object o : Collection){
    if(!executed && (o fulfills condition)){
        executed  = true;
    } 
    //other code
}

Due to Java's short-circuit nature, you will only ever check !executed, and on its second and future runs, that will evaluate to false, thus "skipping" the conditional check.

Makoto
  • 104,088
  • 27
  • 192
  • 230
2

i would go with a different way, like below:

int i = 0;
for ( ; i<collection.size() ; i++)
{
    Object o = collection.get(i);
    if (o fulfills condition)
    {
        // do what you gotta do and then
        break;
    }
    doSomethingElse(o); // define a private method for whatever is done here
}
for ( ; i<collection.size() ; i++)
{
    doSomethingElse(collection.get(i));
}
tt_emrah
  • 1,043
  • 1
  • 8
  • 19
0

Assuming you still want "other code" to keep running, and that the condition test might be slow, flip the tests and the boolean:

boolean doTest = true;
for(Object o : Collection){
    if (doTest && (o fulfills condition)) {
        doTest = false;
    }
    //other code
}

This eliminates the negative, and the && operator prevent the condition from being evaluated once doTest is false.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Not knowing the context, the only improvement I would suggest is to check for executed first -

boolean executed = false;
for(Object o : Collection){
    if(executed  == false && (o fulfills condition) ){
        //do something
        executed  = true;
    } 
    //other code
}

This will have the least side-effects. Otherwise, I don't know how to improve your solution since you say "other-code" still need to execute. Hope this helps.

Prashant
  • 1,002
  • 13
  • 29
0

In order to loop through a list and perform a task only the first time a condition is met, and not test the condition afterwards, you can make use of the short-circuit nature of Java's and operator. See the following code:

boolean performed = false;
for(Object o : Collection){
    if(!performed && o fulfills condition){
        //do something
        performed  = true;
    } 
    //other code
}

After the task is performed, the performed flag will be set to true. When Java realizes the first operand (the condition before the &&) evaluates to true, it doesn't bother calculating o fulfills condition. This is called short-circuiting. This is useful when the second operand (o fulfills condition) is an expensive calculation that you want to avoid performing if it's not necessary.

Parker Hoyes
  • 2,118
  • 1
  • 23
  • 38