0

So I have this piece of code:

void go() {
    String x = "Hi";

    switch (x) {
    case "Hi": 
        System.out.println("Hi");
    }
}

And this returns: Hi

But when I run

void go() {
    String x = "Hi";

    switch (x) {
    case "Hi":
        System.out.println("Hi");
    case "Bye":
        System.out.println("Bye");
    }
}

It returns:

Hi Bye

Why is this.Is there something I missed?

deHaar
  • 17,687
  • 10
  • 38
  • 51

3 Answers3

2

As already stated before, you have to add break; statements for each case if you want to stop at that special one. Your code would then look like this:

void go() {
    String x = "Hi";

    switch (x) {
    case "Hi":
        System.out.println("Hi");
        break;
    case "Bye":
        System.out.println("Bye");
        break;
    }
}

Another thing you really should do is adding a default case for any non matching input (imagine someone input "Hy" instead of "Hi", there wouldn't be any output for that...):

void go() {
    String x = "Hi";

    switch (x) {
    case "Hi":
        System.out.println("Hi");
        break;
    case "Bye":
        System.out.println("Bye");
        break;
    default:
        System.out.println("Your input was \"" + x 
                + "\", please enter either \"Hi\" or \"Bye\" instead!");
    }
}

The default statement is an option for anything that isn't handled in the case statements.

Now back to the breaks... You can handle different cases just the same if you set the breaks only to some of the cases:

void go() {
    String x = "Hi";

    switch (x) {
    case "Hi":
    case "Hy":
        System.out.println("Hi");
        break;
    case "Bye":
    case "By":
        System.out.println("Bye");
        break;
    default:
        System.out.println("Your input was \"" + x 
                + "\", please enter either \"Hi\", \"Hy\", \"By\" or \"Bye\" instead!");
    }
}

Doing like this, you will receive the same output for "Hi" and "Hy" without duplicating the code that handles both cases.

deHaar
  • 17,687
  • 10
  • 38
  • 51
1

You should put some break statements at the end of case statements or the execution will propagate to the next case statements. Even if it is counter intuitive, it is sometimes useful.

switch(int n) {
  case 1: System.out.println("Hello");
  case 2: System.out.println("World");
}

Here switch(1) will display :

Hello
World

And switch(2) will display :

World

But with break statements :

switch(int n) {
  case 1: 
     System.out.println("Hello");
     break;
  case 2: 
     System.out.println("World");
     break;
}

Here switch(1) will display :

Hello

And switch(2) will display :

World
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

In switch statement you have to write break at the end of each case. If you do not write break then it prints all cases.

    switch(value)
    {
       case X:
        //do something
       break;
       case Y:
        //do something
       break;
       default:
       //do something        
      break;
     }
arpita
  • 11
  • 2
  • your answer is incomplete and wrong. break; is not mandatory. sometimes (depending on the requirements) it's even wrong to place them. not placing a break doesn't mean it'll automatically print them all, either, just those until the first break is encountered. – Stultuske Nov 27 '18 at 11:01