mercredi 25 novembre 2015

while statement and if statemen

I have got the code below from Java tutorial Oracle and it works just fine.

I understand how for loop, while and if statement works, however, I'm struggling to understand the while and if statement below.

Could someone please explain to me why everything is as it is in the while and if statement below? your help would be greatly appreciated.

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) {
            if (searchMe.charAt(j++) != substring.charAt(k++)) {
                continue test;
            }
        }
        foundIt = true;
            break test;
    }
    System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}

Aucun commentaire:

Enregistrer un commentaire