CodeFinity
CodeFinity

Reputation: 1350

In following program, what is the purpose of the while loop?

There are no problems with the compilation, but whether or not I have the while loop in place or not, the result is the same. I can't understand why the while loop is included. BTW, this is just an example program from the Java SE tutorial:

public class ContinueWithLabelDemo {

    public static void main(String[] args) {

        String searchMe = "Look for a substring in me";
        String substring = "sub";
        boolean foundIt = false;

        int max = searchMe.length() - substring.length();

        test:
        for (int i = 0; i <= max; i++) {
            int n = substring.length();
            int j = i;
            int k = 0;

            while (n-- != 0) { // WTF???
                if (searchMe.charAt(j++) != substring.charAt(k++)) {
                    continue test;
                }
            }

            foundIt = true;
            break test;
        }
        System.out.println(foundIt ? "Found it" : "Didn't find it");
    }
}

Upvotes: 0

Views: 164

Answers (1)

Pshemo
Pshemo

Reputation: 124225

You can replace your

while (n-- != 0) { // WTF???

with

System.out.println("outside loop");
while (n-- != 0) { // WTF???
    System.out.println("inside loop: comparing "
            + searchMe.charAt(j) + ":" + substring.charAt(k));

to see how this example works. Below is little explanation.


This code is searching for substring in searchMe string. Take a look at this example:

Look for a substring in me
^
sub

If you compare characters at position 0 in searchMe and substring you will notice that they are not the same L != s so we can skip matching rest of letters and go to next position (that is the purpose of continue test;)

Look for a substring in me
 ^
 sub

So now we will try compare next letter with first letter of searchMe with first letter of substring. This time we get o!=s so there is no way that substring starts in this place, lets carry on.

After few comparisons we finally found promising place

Look for a substring in me
           ^
           sub

where first letter of substring is the same as current letter in searchMe (s==s) so we wont jump from while loop yet and will try to check next letter. And we have another success

Look for a substring in me
            ^
           sub

because u==u, so we will continue our loop until we iterate over our entire substring which can happen in next step.

Look for a substring in me
             ^
           sub

And this time we compared b with b. Since they are equal and we don't have more letters in substring to check we can set value of foundIt to true and brake test for loop.

And that is the end.


If you remove while from your code you will get positive response as soon as you will find first character that will match first letter of substring in your case in after checking Look for a program will match s with first letter on substring which will also be s.

While loop is used here to iterate over entire substring and only in case of fail in matching corresponding characters we will move searching one place forward. If we would ignore this inner loop and just iterate over entire data we can ignore some positive results like in case where we would look for aab in aaab String. Take a look

aaab
aab
^^

^ will match but after them we will have to match a with b which will fail. Without inner while loop we would probably start another match from last checked position that failed which would be

aaab
  aab
  ^

This time we also failed to find match for substring so we skipped a*aab* part.

Upvotes: 4

Related Questions