Link to home
Start Free TrialLog in
Avatar of JavaConvert
JavaConvert

asked on

Correctly exiting a for loop...

What is the best way to get out of a for loop

I have :

public boolean find(String name) {
   int i, count = array.count();
   for (i=0;i<count;i++)
     if (array.objectAt(i).equals(name)
       return true;

    return false;
}

Is it alright/good practice to get out of the loop this way? Any downsides to it?

ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sciuriware
sciuriware

Before you post code, please compile it first.

;JOOP!
Avatar of CEHJ
I would prefer


public boolean find(String name) {
   int i, count = array.count();
   boolean found = false;
   for (i=0;i<count && !found;i++)
     found = array.objectAt(i).equals(name);
   return found;
}