I am trying to understand how is it that my else statement is not being accessed in this simple program, basically, what I am trying to do is loop through an array of random numbers, if the item is found text would display demonstrating that it has been found. If it has not been found I used continue to go to the next iteration. If it has not been found at all then a statement would jump explaining that the item was not found on the array.
import java.util.*;
public class practice {
public static void main(String[] args) {
int myA[] = new int[11];
int toS = 59;
Random f = new Random();
for(int j = 0; j < myA.length; j++){
myA[j] = f.nextInt(10);
}
System.out.println("The array has");
for(int x = 0; x < myA.length; x++){
System.out.println(myA[x]);
}
System.out.println("Loop through elements");
System.out.println("loop test");
for(int u = 0; u < myA.length; u++){
if(myA[u] == toS){
System.out.println("Item found" );
break;
}
if(myA[u] != toS){
continue;
}
else{
System.out.println("Item not in list");
break;
}
}// end of loop
}//After this no more code
}
Am I overthinking this? I used the debugger to step through the processes, I saw that the continue statement is what was jumping out from the loop and ending without making a pass to the else statement. How would I fix the iteration? If I remove the continue block the code would only check the first iteration.
Thank you for your time guys.
ifsaysif (myA[u]==toS) { ... break; }. So the iteration can only go on ifmyA[u]is not equal totoS. So clearly theelseclause will never fire.