Search⌘ K

Searching an Array for a Given Entry

Explore how to search for a specific entry in a Java array by using loops and conditional statements. This lesson teaches you to write a loop that stops when the desired item is found, employing Boolean flags and efficient control flow to perform the search effectively.

Problem statement

To see whether an array contains a particular entry, we use a loop much like the ones we used in the previous lessons. Although those loops cycle through the whole array, the loop we need here should stop as soon as the first occurrence of the desired entry is found in the array. Right now, we do not care whether the entry occurs more than once.

Pseudocode

We will now describe the logic we need for this search by developing a pseudocode solution.

First draft pseudocode

The following pseudocode describes the ...