Searching in an Array II

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

Lotto.java
// Check if drawnNumber is in selectedNumbers
boolean found = false;
for (int i = 0; !found && i < MAX_SELECT; ++i) {
   found = drawnNumber == selectedNumbers[i];
}

*This is a typical loop that attempts to find the first occurence of a given value in an array.
 
*found is to be set to true as soon we have found drawnNumber within selectedNumbers.
 
*Note that we terminate the loop immediately once we have found what we are looking for.
 
*This loop construct would not only be slower if we left out found! from the for condition but also return possibly wrong results. Why?
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002