C language learned by suffering
C language learned by suffering
Case Classification by Number
Number and corresponding process
With the methods we have described, it is no longer possible to determine any condition.
However, there is one more useful decision statement provided in the C language that you should be aware of.
In our surroundings, numbering is often used.
As one example, consider a school class attendance number list.
Suppose that the attendance number list for a certain class in a certain school is as follows.
The above is just an example for clarity, and differs from the setting in the work.
Based on the above, let's create a program that displays the corresponding name when the attendance number is entered.
This can be easily accomplished with the aforementioned else-if statement.
The above program works properly.
However, there are so many else-if statements that the program is a bit cumbersome to type.
The switch statement - case can be used to write more beautifully.
The usage of the switch statement ~case is as follows.
The switch statement jumps the order of processing to the case with the same value as the value of the specified conditional expression.
At the jump destination, it executes the statements below the case, and when it finds the break statement
Get out of the enclosed {} after the switch statement.
The following program is an example of a rewritten program using the switch statement ~case.
The correspondence with the number is clearer and more beautiful than before.
Of course, when you enter a number, the corresponding name is displayed.
Note that, unlike an if statement, multiple statements may be arranged without {}.
However, there is one more useful decision statement provided in the C language that you should be aware of.
In our surroundings, numbering is often used.
As one example, consider a school class attendance number list.
Suppose that the attendance number list for a certain class in a certain school is as follows.
Number | Name | Gender |
---|---|---|
1 | Nobi Nobita | Male |
2 | Shizuka Minamoto | Female |
3 | Takeshi Gouda | Male |
4 | Suneo Konekawa | Male |
The above is just an example for clarity, and differs from the setting in the work.
Based on the above, let's create a program that displays the corresponding name when the attendance number is entered.
This can be easily accomplished with the aforementioned else-if statement.
source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
if (no == 1) {
printf("Nobi Nobita\n");
} else if (no == 2) {
printf("Minamoto Shizuka\n");
} else if (no == 3) {
printf("剛田武\n");
} else if (no == 4) {
printf("骨川スネ夫\n");
} else {
printf("No one has such a number\n");
}
return 0;
}
The above program works properly.
However, there are so many else-if statements that the program is a bit cumbersome to type.
A simpler way to write the above is available only when determining a match with more than one number.
The switch statement - case can be used to write more beautifully.
The usage of the switch statement ~case is as follows.
How to use the switch statement ~case
switch (conditional expression) {
case Numeric:
Execution statement;
break;
case number:
Execution statement;
break;
}
The switch statement jumps the order of processing to the case with the same value as the value of the specified conditional expression.
At the jump destination, it executes the statements below the case, and when it finds the break statement
Get out of the enclosed {} after the switch statement.
The following program is an example of a rewritten program using the switch statement ~case.
source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
printf("NOBI NOBITA\n");
break;
case 2:
printf("Minamoto Shizuka\n");
break;
case 3:
printf("剛田武\n"); break; case 3: printf("剛田武\n");
break;
case 4:
printf("骨川スネ夫\n");
break;
}
return 0;
}
The correspondence with the number is clearer and more beautiful than before.
Of course, when you enter a number, the corresponding name is displayed.
Note that, unlike an if statement, multiple statements may be arranged without {}.
Programmers are artists.
We have used words such as beautifully, etc., since a few minutes ago.
In fact, the word is used surprisingly commonly among programmers.
A programmer's job is a puzzle of combining simple calculations into complex ones.
In some aspects, it is almost an art form.
In fact, the word is used surprisingly commonly among programmers.
A programmer's job is a puzzle of combining simple calculations into complex ones.
In some aspects, it is almost an art form.
What to do if none of the above apply?
In fact, if you run the switch to case statement program described in the previous section, you will see that
The result may be different than when created with the else-if statement.
With the else-if statement, if you specify a number that is not in the roster, the
It would have displayed the error as, "No one has that number."
The switch statement to case program does not display anything.
Thus, to make the process run when the other case values do not apply, you can use the
default ( default) can be used.
Default can be used in place of a case statement.
Jump to default if there is no number that fits in the other cases.
The following program adds the default
If you run this program and enter 10, the result will be as follows
If numbers 1-4 are entered, the corresponding names will be displayed correctly.
The result may be different than when created with the else-if statement.
With the else-if statement, if you specify a number that is not in the roster, the
It would have displayed the error as, "No one has that number."
The switch statement to case program does not display anything.
Thus, to make the process run when the other case values do not apply, you can use the
default ( default) can be used.
Default can be used in place of a case statement.
Jump to default if there is no number that fits in the other cases.
The following program adds the default
source code
#include <stdio.h>
int main(void)
{
int no;
scanf("%d", &no);
switch (no) {
case 1:
printf("NOBI NOBITA\n");
break;
case 2:
printf("Minamoto Shizuka\n");
break;
case 3:
printf("剛田武\n"); break; case 3: printf("剛田武\n");
break;
case 4:
printf("骨川スネ夫\n");
break;
default:
printf("No one with such number\n");
break;
}
return 0;
}
If you run this program and enter 10, the result will be as follows
Execution Result
10 Input data
No one has that number.
No one has that number.
If numbers 1-4 are entered, the corresponding names will be displayed correctly.
Putting together a similar process
Next, let's consider a program that displays gender using the previous roster.
This is very easy because all you have to do is rewrite the string.
The following program was created by rewriting the string.
It's going to be another familiar way to consider this program.
The string "male" is used three times, which is very wasteful.
Can this somehow be done only once?
In fact, CASE can be used with more than one in succession.
The case itself has no meaning other than to indicate where to jump in a switch statement.
Even if multiple cases are connected, there is no effect on the content of execution.
The following program is a combination of cases 1, 3, and 4.
In this program, if 1, 3, or 4 is entered, the
Both display "male".
This is very easy because all you have to do is rewrite the string.
The following program was created by rewriting the 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("女性\n"); break; case 2: printf("女性\n");
break;
case 3:
printf("男性\n"); break; case 3: printf("男性\n");
break;
case 4:
printf("男性\n"); break; case 4: printf("男性\n");
break;
default:
printf("No one with such number\n");
break;
}
return 0;
}
It's going to be another familiar way to consider this program.
The string "male" is used three times, which is very wasteful.
Can this somehow be done only once?
In fact, CASE can be used with more than one in succession.
The case itself has no meaning other than to indicate where to jump in a switch statement.
Even if multiple cases are connected, there is no effect on the content of execution.
The following program is a combination 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("女性\n"); break; case 2: printf("女性\n");
break;
default:
printf("No one with such number\n");
break;
}
return 0;
}
In this program, if 1, 3, or 4 is entered, the
Both display "male".
Be careful with the break statement.
As shown in this example, cases can be connected without a break statement.
Conversely, forgetting the break statement will cause unrelated cases to be connected.
There is an actual example of a well-known application that caused a serious bug by forgetting the break statement.
Be very careful with the break statement.
Conversely, forgetting the break statement will cause unrelated cases to be connected.
There is an actual example of a well-known application that caused a serious bug by forgetting the break statement.
Be very careful with the break statement.
Weak judgment
As you can see from what we have seen so far, using the switch statement ~case
It is easy to write programs that branch in multiple directions.
In fact, however, the switch statement ~case is very weak in judgment.
In the switch statement ~case, only integer values can be placed in the case
Real numbers, variables, and conditional expressions are not allowed.
In other words, it is not possible to compare variables or to compare large and small relationships, as is the case with if statements.
The switch statement ~case can only be used to compare variables and integer values.
When complex decisions are required, there is no other way but to use if statements.
It is easy to write programs that branch in multiple directions.
In fact, however, the switch statement ~case is very weak in judgment.
In the switch statement ~case, only integer values can be placed in the case
Real numbers, variables, and conditional expressions are not allowed.
In other words, it is not possible to compare variables or to compare large and small relationships, as is the case with if statements.
The switch statement ~case can only be used to compare variables and integer values.
When complex decisions are required, there is no other way but to use if statements.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.