# Lesson 5.6. Break keyword in switch..case

In the previous lesson on the switch..case, you probably noticed that each case case ends with a `break` statement. This statement ends the execution of the case and exits the the `switch` statement.

When the break is omitted, all cases are executed (even if the test is false) until :

• a `break` is encountered;
• or the switch is terminated.

## Example

Consider the following example where the developer forgot to put the `break`:

``````switch (code) {
case 32 : printf ("Belgium");
case 33 : printf ("France");
case 41 : printf ("Switzerland");
default : printf ("Unrecognized code");
}``````
• If `code` is 1, the program displays `CanadaBelgiumFranceSwitzerlandUnrecognized code`
• If `code` is 32, the program displays `BelgiumFranceSwitzerlandUnrecognized code`
• If `code` is 33, the program displays `FranceSwitzerlandUnrecognized code`
• If `code` is 41, the program displays `SwitzerlandUnrecognized code`
• If `code` is not equal to any of the cases, the program displays `Unrecognized code`.

## Exercises

### Exercise 1

Modify the code of the previous exercise so that it can convert upper and lower case characters. Be smart and use the concepts of this course.

``````// Character to convert
char hex = 'B';
// Result in base 10
short dec;

// Convert hex to decimal (base 10)
// MODIFY THE SWITCH..CASE
// TO ACCEPT LOWERCASE CHARACTERS
switch (hex) {
case '0': dec=0; break;
case '1': dec=1; break;
case '2': dec=2; break;
case '3': dec=3; break;
case '4': dec=4; break;
case '5': dec=5; break;
case '6': dec=6; break;
case '7': dec=7; break;
case '8': dec=8; break;
case '9': dec=9; break;
case 'A': dec=10; break;
case 'B': dec=11; break;
case 'C': dec=12; break;
case 'D': dec=13; break;
case 'E': dec=14; break;
case 'F': dec=15; break;
default : dec = -1;
}

// Display the conversion result
if (dec==-1) printf ("Non valid hexadecimal number\n");
else printf ("0x%c = %d in decimal\n", hex, dec);``````

Here is the expected display for `char hex = 'f';` :

``0xf = 15 in decimal``

### Exercise 2

Using a `switch..case`, write a program that displays whether the `letter` variable is a consonant or a vowel. We will assume that the variable `letter` can only contain a lowercase letter and nothing else. The rule is as follows (according to the answer of the French Academy):

• a, e, i, o and u are vowels;
• y is a vowel and a semi-consonant;
• all other letters are consonants.
``````char letter = 'o';

// Displays whether the letter is a vowel or a (semi)consonant
// COMPLETE HERE``````

Here is the expected display for `char letter = 'o';`

``o is a vowel``

Here is the expected display for `char letter = 'z';`

``z is a consonant``

Here is the expected display for `char letter = 'y';`

``````y is a semi-consonant
y is a vowel``````

## Quiz

What does the following code display?

``````int x = 0;
switch (x) {
case 0: printf ("Bonjour");
case 1: printf ("Hola");
case 2: printf ("Konnichiwa");
default : printf ("Hello");
}``````

What does the following code display?

``````int x = 0;
switch (x) {
case 0: printf ("Bonjour");
case 1: printf ("Hola"); break;
case 2: printf ("Konnichiwa");
default : printf ("Hello");
}``````

Which statements are true?