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 :
break is encountered;Consider the following example where the developer forgot to put the break:
switch (code) {
case 1 : printf ("Canada");
case 32 : printf ("Belgium");
case 33 : printf ("France");
case 41 : printf ("Switzerland");
default : printf ("Unrecognized code");
}
code is 1, the program displays CanadaBelgiumFranceSwitzerlandUnrecognized codecode is 32, the program displays BelgiumFranceSwitzerlandUnrecognized codecode is 33, the program displays FranceSwitzerlandUnrecognized codecode is 41, the program displays SwitzerlandUnrecognized codecode is not equal to any of the cases, the program displays Unrecognized code.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
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):
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
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");
}
break, all cases are executed, even if they are false.
Try again...
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");
}
break, the switch is terminated.
Try again...
Which statements are true?
break, we can mutualize several cases, which can be interesting in terms of performance or code clarity.
Try again...