Case by case basis
Mapping to numbers
With the methods described so far, you can now make judgments on any condition.
However, let's also learn about another convenient conditional statement provided in C.
We often need to number things around us.
Let's consider, as one example, a class attendance roster at school.
Let's assume the attendance roster for a class in a school is as follows.
Please note that the above is merely an example for clarity and does not reflect the actual setting within the story.
Based on the above, let's create a program that displays the corresponding name when an attendance number is entered.
This can be easily achieved with the aforementioned else-if statement.
The program above functions correctly.
However, there are many else-if statements, and the program's input is a bit cumbersome.
A simpler way to write this is only when determining matches with multiple numbers.
You can write more beautifully by using a `switch` statement and `case` labels.
The usage of switch and case statements is as follows.
A switch statement jumps to the case with a value matching the value of the specified condition expression.
At the jump destination, the statements following the `case` are executed until a `break` statement is encountered.
Exit from the curly braces {} after the switch statement.
The following program is an example rewritten using a switch-case statement.
It's clearer and more aesthetically pleasing now, with better alignment to the numbers.
Of course, entering a number will display the corresponding name.
Unlike if statements, you can place multiple statements without using {} brackets.
However, let's also learn about another convenient conditional statement provided in C.
We often need to number things around us.
Let's consider, as one example, a class attendance roster at school.
Let's assume the attendance roster for a class in a school is as follows.
| Number | name | Gender |
|---|---|---|
| 1 | James | Male |
| 2 | Olivia | Woman |
| 3 | Michael | Male |
| 4 | Thomas | Male |
Please note that the above is merely an example for clarity and does not reflect the actual setting within the story.
Based on the above, let's create a program that displays the corresponding name when an attendance number is entered.
This can be easily achieved with the aforementioned else-if statement.
Source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
if (no == 1) {
printf("James\n");
} else if (no == 2) {
printf("Olivia\n");
} else if (no == 3) {
printf("Michael\n");
} else if (no == 4) {
printf("Thomas\n");
} else {
printf("There's no one with that number.\n");
}
return 0;
}
The program above functions correctly.
However, there are many else-if statements, and the program's input is a bit cumbersome.
A simpler way to write this is only when determining matches with multiple numbers.
You can write more beautifully by using a `switch` statement and `case` labels.
The usage of switch and case statements is as follows.
switch statement ~ how to use case
switch (conditional expression) {
case numerics:
runSentence;
break;
case numerics:
runSentence;
break;
}
A switch statement jumps to the case with a value matching the value of the specified condition expression.
At the jump destination, the statements following the `case` are executed until a `break` statement is encountered.
Exit from the curly braces {} after the switch statement.
The following program is an example rewritten using a switch-case statement.
Source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
printf("James\n");
break;
case 2:
printf("Olivia\n");
break;
case 3:
printf("Michael\n");
break;
case 4:
printf("Thomas\n");
break;
}
return 0;
}
It's clearer and more aesthetically pleasing now, with better alignment to the numbers.
Of course, entering a number will display the corresponding name.
Unlike if statements, you can place multiple statements without using {} brackets.
Programmers are artists.
I've been using words like beautifully since earlier, but
surprisingly, these are actually quite common terms among programmers.
A programmer's work is a puzzle of complexly intertwined simple calculations,
and in some respects, it can be said to be akin to art.
surprisingly, these are actually quite common terms among programmers.
A programmer's work is a puzzle of complexly intertwined simple calculations,
and in some respects, it can be said to be akin to art.
Handling cases that don't fit
Actually, when you execute the program with the switch statement and case statements mentioned in the previous section,
The result may differ when created with an else-if statement.
When using an else-if statement, if a number not on the list is specified,
It incorrectly displayed that there was no one with that number, but...
The switch-case program isn't displaying anything.
This allows you to execute a process when the value of another case doesn't match.
You can use the default.
The `default` can be used as an alternative to a `case` statement.
The `default` label is jumped to when no other `case` matches the value.
The following program has been modified with the default added.
If you run this program and input 10, the result will be as follows:
If you enter numbers 1 through 4, the corresponding names will be displayed correctly.
The result may differ when created with an else-if statement.
When using an else-if statement, if a number not on the list is specified,
It incorrectly displayed that there was no one with that number, but...
The switch-case program isn't displaying anything.
This allows you to execute a process when the value of another case doesn't match.
You can use the default.
The `default` can be used as an alternative to a `case` statement.
The `default` label is jumped to when no other `case` matches the value.
The following program has been modified with the default added.
Source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
printf("James\n");
break;
case 2:
printf("Olivia\n");
break;
case 3:
printf("Michael\n");
break;
case 4:
printf("Thomas\n");
break;
default:
printf("There's no one with that number.\n");
break;
}
return 0;
}
If you run this program and input 10, the result will be as follows:
Results
10 Input Data
There's no one with that number.
There's no one with that number.
If you enter numbers 1 through 4, the corresponding names will be displayed correctly.
Group similar tasks.
Next, let's consider a program that displays gender using the roster we just created.
This is very simple, as it only involves rewriting the string.
The following program is a modified version of a string.
Once again, we'll be using a familiar approach, but looking at this program...
The word male is used three times, which is quite redundant.
Is there any way to get this done in one go?
In fact, it is possible to use multiple cases consecutively.
The `case` itself only signifies the jump location within a `switch` statement.
Connecting multiple cases does not affect the execution.
The following program is a concatenation of cases 1, 3, and 4.
In this program, when 1, 3, and 4 are entered,
All display as male.
This is very simple, as it only involves rewriting the string.
The following program is a modified version of a string.
Source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
printf("Male\n");
break;
case 2:
printf("Woman\n");
break;
case 3:
printf("Male\n");
break;
case 4:
printf("Male\n");
break;
default:
printf("There's no one with that number.\n");
break;
}
return 0;
}
Once again, we'll be using a familiar approach, but looking at this program...
The word male is used three times, which is quite redundant.
Is there any way to get this done in one go?
In fact, it is possible to use multiple cases consecutively.
The `case` itself only signifies the jump location within a `switch` statement.
Connecting multiple cases does not affect the execution.
The following program is a concatenation of cases 1, 3, and 4.
Source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
case 3:
case 4:
printf("Male\n");
break;
case 2:
printf("Woman\n");
break;
default:
printf("There's no one with that number.\n");
break;
}
return 0;
}
In this program, when 1, 3, and 4 are entered,
All display as male.
Beware of the break statement.
As shown in this example, you can chain cases together as long as you don't use a break statement. Conversely, forgetting a break statement will cause unrelated cases to be executed together. There are real-world examples where forgetting a break statement caused major bugs in well-known applications. Be sure to pay close attention to break statements.
Lack of judgment
As we've seen, using a switch statement with case labels...
It's easy to write programs with multiple branching paths.
However, the truth is that switch statements and their case clauses have very limited discernment.
In a switch statement, a case can only contain integer values.
It's not possible to include real numbers, variables, or conditional statements.
In other words, it's not possible to compare variables to each other, such as with an if statement, or to perform comparisons of greater than or less than.
The switch statement and its case labels can only be used to compare a variable with integer values.
When complex decisions are required, there's no alternative to using if statements.
It's easy to write programs with multiple branching paths.
However, the truth is that switch statements and their case clauses have very limited discernment.
In a switch statement, a case can only contain integer values.
It's not possible to include real numbers, variables, or conditional statements.
In other words, it's not possible to compare variables to each other, such as with an if statement, or to perform comparisons of greater than or less than.
The switch statement and its case labels can only be used to compare a variable with integer values.
When complex decisions are required, there's no alternative to using if statements.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




