jeudi 21 octobre 2021

How to interpret an if statement inside the while loop body?

i find it difficult to interpret an if statement inside a while loop body. The code i am trying to understand is:

public int findFirst(String searchString)
    {
        int index = 0;
        // Record that we will be searching until a match is found.
        boolean searching = true;

        while(searching && index < files.size()) {
            String filename = files.get(index);
            if(filename.contains(searchString)) {
                // A match. We can stop searching.
                searching = false;
            }
            else {
                // Move on.
                index++;
            }
        }
        if(searching) {
            // We didn't find it.
            return -1;
        }
        else {
            // Return where it was found.
            return index;
        }
    }

Can someone please explain how i shall interpret that method ? All the help that i can get is much appreciated !

Aucun commentaire:

Enregistrer un commentaire