Lacovara
Lacovara

Reputation: 11

Last break in switch unnecessary?

I was musing over a switch statement I had written for 4 mutually exclusive cases. I inserted a break statement at the end of each case, because I didn't want to do the test again after one case or another had been successful.

Here's the question, though. What, if anything, does the last break do? If the test case 43 succeeds, the break terminates the case, but if there's no break, the default shouldn't run, and so there's no wasted test anyway.

Here's the code:

switch(telemetry) {
    case(40):
        printf("\nHouse Telemetry #%i \n", psc);
        break;
    case(41):
        printf("\nNav Telemetry #%i \n", psc);
        break;
    case(42):
        printf("\nDownhill Telemetry #%i \n", psc);
        break;
    case(43):
        printf("\nRealTime Telemetry #%i \n", psc);
        break; // what do I do?
    default:
        printf("\nCommand ID not recognized\n");
}

Perhaps one of you compiler gurus can advise me.

Upvotes: 0

Views: 249

Answers (3)

Eric Postpischil
Eric Postpischil

Reputation: 223593

A switch statement is not a language construction that divides codes into separate cases. It is a computed “go to” statement: Based on the switch expression, it causes program control to jump to one of the labels.

Once that jump is completed, the switch does not exercise any further control over execution. It does not separate cases from one another or insert jumps or returns at the end of each case. Therefore, if you want control to leave the code for one case and not flow into the following code for another case, you must insert a break or other instruction that affects program control (such as return).

This characteristic of switch statements makes possible uses such as:

switch (letter)
{
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        printf("Vowel.\n");
        break;
    default:
        printf("Consonant.\n");
        break;
}

Observe there are five case labels using one section of code. If the switch automatically separated them, the first four would be empty; nothing would be done for them when letter was a, e, i, or o.

Another example is where one case does some preparation work and then falls into another case, such as:

switch (operation)
{
    case Subtract:
        b = -b;
    case Add:
        result = a+b;
        break;
    case Multiply:
        result = a*b;
        break;
}

Here the Add case adds two numbers, and the Subtract case works by negating b and then continuing into the code for the Add case. This could not work if the switch automatically separated cases. (This is a simplified example, of course.)

Upvotes: 1

abelenky
abelenky

Reputation: 64710

If you did not have the break; in case 43, it would fall through to the default case, and also execute the printf("\nCommand ID not recognized\n");.

Why do you think the default would not run? switch-case will continue execution until it encounters a break statement, and this includes the default section.

The break therefore is required for the desired behavior.


Putting a break; on the very last element of a switch-case (whether it is default or case) is optional, as it would stop execution that is coming to an end anyway.

I've encountered programmers who feel both ways about it: That there should not be an extra break at the very end of the block because it is not necessary, or that there should be one for consistency, and on the possibility that the sections get re-arranged in the future.

Upvotes: 5

MED LDN
MED LDN

Reputation: 669

The break keyword in each case indicates the end of a particular case. If we do not put the break in each case then even though the specific case is executed, the switch in C will continue to execute all the cases until the end is reached. This should not happen; hence we always have to put break keyword in each case. Break will terminate the case once it is executed and the control will fall out of the switch.

Upvotes: 0

Related Questions