Java loops and iteration concept

https://www.codecademy.com/courses/learn-java/lessons/learn-java-loops/exercises/break-and-continue

//code:-

public static boolean checkForJacket(String lst) {
for (int i = 0; i < lst.length; i++) {
System.out.println(lst[i]);
if (lst[i] == “jacket”) {
return true;
}
}
return false;
}

public static void main(String args) {
String suitcase = {“shirt”, “jacket”, “pants”, “socks”};
System.out.println(checkForJacket(suitcase));
}

why are we returning false in “checkForJacket” method ?

if the loop finished (meaning "jacket" was not found in the array),
then something still has to be returned;
false indicates "jacket" was not found,
so
return false;

1 Like