1

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.

1
  • The logic in your loop doesn't make sense. The first if says if (myA[u]==toS) { ... break; }. So the iteration can only go on if myA[u] is not equal to toS. So clearly the else clause will never fire. Commented Oct 22, 2015 at 9:47

3 Answers 3

4

you should not use continue in your loop.

    boolean itemFound = false;
    for(int u = 0; u < myA.length; u++){
            if(myA[u] == toS){
                itemFound = true;
                break;
            } 
        }// end of loop

    if (itemFound) {
        System.out.println("Item found" );
    } else {
        System.out.println("Item not in list");
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You else statement is not entered because it is already covered by the previous if statement, which breaks from the loop :

        if(myA[u] == toS){
            System.out.println("Item found" );
            break;
        }
        if(myA[u] != toS){
            continue;
        }
        else{ // myA[u] == toS - never happens, since previous if statement broke from the loop
            System.out.println("Item not in list");
            break;
        }

Your logic can be handled this way, using a boolean variable :

    boolean found = false;
    for(int u = 0; u < myA.length; u++){
        if(myA[u] == toS){
            System.out.println("Item found" );
            found = true;
            break;
        }
    }
    if (!found) {
        System.out.println("Item not in list");
        break;
    }

Note that only after the loop ends you can check if the item was found or not.

1 Comment

Thank you for the great explanation. I was able to go from your explanation and solve additional portions of the code I was using in a gui tool project I am working on. I got caught in this problem with arrays and could not get out of ti, your example gave me better insight.
0

look at the following snippet

  if(myA[u] == toS){
         System.out.println("Item found" );
         break;
    }
   if(myA[u] != toS){
                    continue;
   }

in any possible case either toS will be equal to myA[u] or not equal to it,

if it is equal then as 'break' statement gets executed and it comes out of the for loop , and if it is not equal 'continue' statement gets executed and so it comes out of normal iteration loop

so your else loop will never run ,as there is no condition in which one of the 2 if loops wont execute

hope this helps!

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.