2

I want to know something of the differentia when I use break or return in switch statement. And which is more efficient? Is anyone can explain this to me? Thank you very much. And the code is below:

public void foo(){
    int i = 10;
    switch (i) {
        case 1: {
            //code here
            break;  //or return;
        }
        case 2: {
            //code here
            break;  //or return;
        }
        // code  code
        // code  code
        // code  code
        case 10:{
            //code here
            break;  //or return;
        }
        default:{
            //code here
            break;  //or return;
        }
    }
}
秃德666
  • 395
  • 1
  • 3
  • 15
  • 6
    There's no difference in your case. In non-void methods, they do different things. – shmosel Sep 30 '16 at 03:43
  • I know there is no different of the function,but i want to know which is more efficient. – 秃德666 Sep 30 '16 at 05:44
  • 1
    Why would you think one is more efficient? Even assuming there were a performance difference, you spent more time asking this question than you'll ever save from your imaginary optimization. Find something better to do. – shmosel Sep 30 '16 at 05:51
  • ok, thx, just because my co-worker change break to return in my code. So I just want to know if this is more efficient. – 秃德666 Sep 30 '16 at 06:03
  • I would tell your co-worker to stop fiddling with your code. – Stephen C Sep 30 '16 at 07:28

4 Answers4

6

The two are quite different - trying to compare them in terms of efficiency is simply nonsensical. A break terminates execution of the consequent clauses of the switch and continues execution after the switch block. A return terminates execution of the current method and returns control to the calling method. The only thing they have in common is that the code will not fall through to execute any further cases.

In general, I would be wary of using a return out of a switch statement, not because of any effect on performance but because it makes it a little harder to reason about the behavior of the code. (I would also argue that the switch statement in Java is almost never needed, and its presence in a program generally indicates a design failure, but that's another question)

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
3

In this example there is no difference in meaning or in performance. In other case, there could be a difference ... because break and return mean different things. (And if two construct mean different things in a given context, they are likely to perform differently!)

However, stylistically break is better:

  1. In general1, methods with multiple exit points (i.e. multiple explicit and implicit return statements) are harder to understand.

  2. In this example, suppose that you decided that you wanted to change the method to do something after the switch statement ....


1 - This is debatable (see https://stackoverflow.com/a/4838892/139985), and there are exceptions. But I would say that it applies in this example.


ok, thx, just because my co-worker change break to return in my code. So I just want to know if this is more efficient.

I would ask them to explain why they did it. The point is that they might have done it for a good reason, or a debatable reason ... or an incorrect reason. If they did it for an incorrect reason, they need understand that their reasoning was incorrect, so that they don't waste everyone's time and possibly damage the code-base in the process while making changes like this.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Very debatable. Personally, I strongly prefer multiple returns in switch inside a non-void method, as it shortens the code and reduces boilerplate. – shmosel Sep 30 '16 at 05:55
  • Actually, in this example, it does not save **any** boiler plate. Since `break` is one character less than `return`, you are >>saving<< space by using `break`. (Not that ite matters. Just saying ...) – Stephen C Sep 30 '16 at 07:26
  • This example is not a non-void method. My point is that it's very subjective and depends on the circumstances. – shmosel Sep 30 '16 at 08:01
1

shmosel is right in your case. But if you want to say do an operation in the switch statement and return that value you would need a return because you are getting something back from the switch. A break just stops the execution of the Switch so nothing is returned.

There is no real impact on efficacy between the two different version. They just behave a little differently. The memory that is taken up will be similar.

MNM
  • 2,673
  • 6
  • 38
  • 73
-2

break mean the code will exit current block ({ }) and continue to execute following code, if there are any.

int i = 1;
   switch (i)
   {
      case 1: Console.WriteLine("1"); break;
      case 2: Console.WriteLine("2"); break;
      default: Console.WriteLine("..."); break;
   }

   Console.ReadLine();

In the above case, the console will wait for user input since it able to reach Console.ReadLine();

int i = 1;

   switch (i)
   {
      case 1: Console.WriteLine("1"); return;
      case 2: Console.WriteLine("2"); break;
      default: Console.WriteLine("..."); break;
   }
Console.ReadLine();

As for the case above, the program will not execute any following code and it will never reach Console.ReadLine() when int i is 1.

You can see their behavior clearly by debugging them.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49