What is the difference between return and break in a switch case?

9

In some situations you need to practice switch case for optimization and improvement of code. I have an application developed for Android using Java, which in the case is used in both situations. Below I have an example using return which is used with Tabs :

switch (position){
    case 0:
        Tab tab1 = new Tab1();
        return tab1;
    case 1:
        Tab tab2 = new Tab2();
        return  tab2;
    case 2:
        Tab tab3 = new Tab3();
        return tab3;
}

In this case below we have to use the break in which the desired month is defined:

    int month = 8;
    String monthString;
    switch (month) {
        case 1:  monthString = "January";
                 break;
        case 2:  monthString = "February";
                 break;
        case 3:  monthString = "March";
                 break;
        case 4:  monthString = "April";
                 break;
        case 5:  monthString = "May";
                 break;
        case 6:  monthString = "June";
                 break;
        case 7:  monthString = "July";
                 break;
        case 8:  monthString = "August";
                 break;
        case 9:  monthString = "September";
                 break;
        case 10: monthString = "October";
                 break;
        case 11: monthString = "November";
                 break;
        case 12: monthString = "December";
                 break;
        default: monthString = "Invalid month";
                 break;
    }

In gringo OS, you have an explanation (en) , but I did not understand exactly what which diverges from each other. What real difference between return and break in switch case ?

    
asked by anonymous 05.01.2017 / 17:56

3 answers

17

The return ends the execution of the method regardless of where it is and returns the value.

The break force (manually) the output of a loop or conditional in the case of switch .

In the second code if there is something else to be executed then switch will be executed, if it was a return as in the first example the method would end up right there.

The example below shows that in this context the use of break and return is exactly the same, once made the choice the method ends no other action happens.

As for the use of the most suitable for the situation some important points to think about are:

  • Do I need to return value?

  • When choosing return should I use the single return technique (SESE)?

Recommended reading:

Why should I only use a "return" on each function?

Example - ideone

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println("Com Break: "+ comBreak(1));
        System.out.println("Com Return: "+ comReturn(1));
    }

    public static String comBreak(int mes){
        String mesExtenso = "";
        switch (mes){
            case 1: 
                mesExtenso = "Janeiro";
                break;
            case 2: 
                mesExtenso = "Fevereiro";
                break;              
            case 3: 
                mesExtenso = "Março";
                break;              
            default: 
                mesExtenso = "Mês inválido";
                break;              
        }
        System.out.println("Instruções executadas antes do return");
        return mesExtenso;
    }


    public static String comReturn(int mes){

        switch (mes){
            case 1: 
                return "Janeiro";
            case 2: 
                return "Fevereiro";
            case 3: 
                return "Março";
            default: 
                return "Mês inválido";
        }
        //Qualquer instrução daqui para baixo gera o erro "unreachable statement"
        //return "Forever alone :( ....";

    }   

}
    
05.01.2017 / 18:01
10

The break , only terminates the execution of switch and goes to the next statement after it, is essentially the same as that occurring in a loop. The return does nothing special inside it, it terminates the execution of the function where that code is.

The answer accepted there in the OS is confusing and almost wrong.

    
05.01.2017 / 18:00
3

I'm sorry to disagree with the 'almost wrong'.

The response in the OS says that RETURN returns immediately to the point where it was called while break only exits the loop without returning - ie the function could continue doing other things before returning. >

What, in my opinion, is correct.

    
05.01.2017 / 18:06