In C, there is a conditional operator (switch..case
) to compare
a variable with several expressions.
The general syntax is:
switch ( Variable ) {
case Value1: Instruction block 1;
break;
case Value2: Instruction block 2;
break;
...
case ValueN: Instruction block N;
break;
default: Default instruction block;
}
Variable
is equal to Value1
, then instruction block 1
is executed.Variable
is equal to Value2
, then instruction block 2
is executed.Variable
is equal to ValueN
, then instruction block N
is executed.Variable
is not equal to any of the valueX
, the default block of instructions
is executed. Here is the associated flowchart:
switch..case
can't deal with intervals.Here is an example that repeats a previous exercise.
This time the sequence of if
is replaced by a single switch..case
:
// Display numbers in words
switch (number) {
case 0: printf ("zero\n"); break;
case 1: printf ("one\n"); break;
case 2: printf ("two\n"); break;
case 3: printf ("three\n"); break;
case 4: printf ("four\n"); break;
case 5: printf ("five\n"); break;
case 6: printf ("six\n"); break;
case 7: printf ("seven\n"); break;
case 8: printf ("eight\n"); break;
case 9: printf ("nine\n"); break;
default : printf ("%d is not a number between 0 and 9\n", number);
}
Write a program that asks the user to enter a number. Using the switch..case
statement display the number spelled out in French.
This page
list the spelling of French number.
unsigned int number;
// Asks the user to enter a number
printf ("Enter a number between 0 and 9 : ");
scanf ("%u", &number);
// Displays the number in full in French
// COMPLETE HERE
Here is the expected display:
Enter a number between 0 and 9 : 7
sept
Enter a number between 0 and 9 : 12
12 is not a number between 0 and 9.
The variable number
is now of type char
and contains a character.
If the character represents a number (from 0 to 9), the program displays this number in
full in French. If it is not a number, we display : Non valid character.
char number = '7';
// Displays the number in full in French
// COMPLETE HERE
Here is the expected display with char number = '7';
sept
Here is the expected display with char number = 'x';
x is not a character between 0 and 9.
Write a program that converts a hex
character containing a hexadecimal symbol
(from 0 to F). The result of the conversion in decimal will be assigned to the variable dec
. We
process only uppercase characters for the letters from A to F. If the character is not valid,
we put -1 in the variable dec
.
// Character to convert
char hex = 'B';
// Result in base 10
short dec;
// Convert hex to decimal (base 10)
// COMPLETE HERE
// 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 with char hex = 'F';
0xF = 15 in decimal
Here is the expected display with char hex = 'f';
Non valid hexadecimal number
What does the following code display?
int x = 2;
switch (x) {
case 0: printf ("Bonjour"); break;
case 1: printf ("Hola"); break;
case 2: printf ("Konnichiwa"); break;
default : printf ("Hello");
}
x=2
, the case 2:
is executed.
Try again...
What does the following code display?
int x = 3;
switch (x) {
case 0: printf ("Bonjour"); break;
case 1: printf ("Hola"); break;
case 2: printf ("Konnichiwa"); break;
default : printf ("Hello");
}
x
does not correspond to any of the cases. The default instruction is executed.
Try again...
What syntax is used to test a range with a switch..case
?
switch (grade) {
//...
??? : printf ("Higest honors"); break;
//...
}
switch..case
.
Try again...