0

Please refer the below code in which loop break does not work inside the switch block, can you help?

    String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };

    //For loop is not breaking after getting the correct value
    for (int t = 0; t < Books.length; t++) {
      switch (Books[t]) {
        case "Harry Potter":
             System.out.println("Getting from switch case " + t + " " + Books[t]);
            break;
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
    }
myflash
  • 21
  • 1
  • 8
  • 2
    please format your code. – Omore Apr 01 '17 at 20:37
  • The solution of using return works as long as you are returning the value found, if the method where the loop is returns void there will be no difference between found and not found, you will need to put extra checks in place, like checking a found flag or for !=null – bichito Apr 01 '17 at 20:58
  • 1
    You only break `switch` not `for` – Yu Jiaao Apr 02 '17 at 03:03

2 Answers2

3

break when used inside the switch statement breaks ONLY the switch flow, but not the for loop, so If you wanted to break the for loop, use return when the correct value is found which will break the loop and returns from the method, as shown below:

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
      switch (Books[t]) {
        case "Harry Potter":
             System.out.println("Getting from switch case " + t + " " + Books[t]);
            return;//use return when CORRECT CONDITION is found
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
    }

In simple terms, your break will be applied to the inner code block which is a switch here, so it doesn't break the for loop. So, if you wanted to break both switch & for together, use return statement so that it returns from the method.

One important point is that do not use the labels (to jump between lines) in the code which is against to the structured programming.

OPTION(2):

If you don't want to return from the method, you need to refactor your code and move the book finding logic to a separate method like checkBookExists as shown below:

private boolean checkBookExists(String book, int t) {
      boolean bookFound = false;
      switch (book) {
        case "Harry Potter":
            bookFound = true;
            System.out.println("Getting from switch case " + t + " " + book);
            break;
        default:
            System.out.println("Invalid search for book from switch case");
            break;
        }
        return bookFound;
   }

Now call that checkBookExists method inside the for loop as shown below and when the book is found, for will break.

String Books[] = { "Harry Potter", "To Kill a Mocking Bird", "Hunger Games" };
    for (int t = 0; t < Books.length; t++) {
        if(checkBookExists(Books[t], t)) {
            break;
        }
    }
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Thanks for your solution! This is also great – myflash Apr 01 '17 at 21:21
  • I recommend either of these options than labels, which are bad practice, look here: http://stackoverflow.com/questions/11133127/why-it-is-a-bad-practice-to-use-break-continue-labels-in-oop-e-g-java-c – Vasu Apr 01 '17 at 21:22
  • Nice solution , if you find his solution useful accept it to be as reference in the future – Oghli Apr 01 '17 at 22:07
  • @javaguy i like this point " do not use the labels (to jump between lines) in the code which is against to the structured programming." important note and you are right jump label is preferred to use only with low level languages like assembly and it's necessary to work with it in low level programming. – Oghli Apr 01 '17 at 22:12
1

Well, that break will only break out of the switch statement. You can try using a labeled break, e.g.

loop:
for (int t = 0; t < Books.length; t++ ) {
    // ...
    case:
        // ...
        break loop;

Or, you could put the loop in its own method instead and use a return statement.

csirmazbendeguz
  • 598
  • 4
  • 13