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?
I have :
public boolean find(String name) {
int i, count = array.count();
for (i=0;i<count;i++)
if (array.objectAt(i).equals(
return true;
return false;
}
Is it alright/good practice to get out of the loop this way? Any downsides to it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(n ame);
return found;
}
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(n
return found;
}
;JOOP!