- ..clause 1..: Processing to make them correspond to number
- ..clause 2..: Processing when not applying
- ..clause 3..: Bring similar processing together.
- ..clause 4..: Weakness of judgment
The judgment of any condition is also already possible by the method of the explanation before.
In our surroundings, there is what does the number division well.
Let's see the attendance number list of names of the class at the school by thinking as the one example.
Assume the attendance number list of names of the class where a certain school exists is as follows.
| Number | Name | Sex |
|---|---|---|
| 1 | Nobi postponing Futoshi | Man |
| 2 | [Minamotoshizuka] | Woman |
| 3 | Great physical strength [dentakeshi] | Man |
| 4 | Bone river [sune] husband | Man |
It is not so beautiful in this method though is of course good continue else-if either.#include <stdio.h> int main (void) { int no; scanf("%d",&no); If (no == 1) Printf (..".. \n .. postponing Nobi.. ..fat.. ....". ); } else if (no == 2) { Printf ("[Minamotoshizuka] \n"); } else if (no == 3) { Printf ("Great physical strength [dentakeshi] \n"); } else if (no == 4) { Printf ("Bone river [sune] husband \n"); } else { Printf ("The person of such a number is \n that not is"); } return 0; }
The switch sentence jumps to case of the same value as the value of the specified conditional expression.Case numerical value: Executable statement; break; Case numerical value: Executable statement; break; }
Correspondence with the number is comprehensible, and is more beautiful than ahead.#include <stdio.h> int main (void) { int no; scanf("%d",&no); switch (no) { case 1: Printf ("\n ..postponing Nobi.. ..fat.."); break; case 2: Printf ("[Minamotoshizuka] \n"); break; case 3: Printf ("Great physical strength [dentakeshi] \n"); break; case 4: Printf ("Bone river [sune] husband \n"); break; } return 0; }
[ The programmer is an artist ].
Ahead though the word beauty etc. are used
Actually, it is a word usually used among programmers by contraries.
The programmer's work is a puzzle that combines the simple mathematics with the complexity.
It should be said that it is near the artist rather than the engineer.
Actually, when the program of switch sentence-case sentence in the preceding clause is executed and seen
The result might be different from the time made by the else-if sentence.
When you specify the number that doesn't exist in the list of names at the else-if sentence
, saying that "There is no person of such a number" though the mistake was displayed
In the program of switch sentence- case, do not display anything.
Thus, to execute processing when not applying to the value of other case
Default (default) can be used.
Default can be used as taking the place of the case sentence.
It jumps when in default, there is no numerical value that applies to other case.
The following program is an addition of default.
The result when this program is executed and ten is input becomes as follows.#include <stdio.h> int main (void) { int no; scanf("%d",&no); switch (no) { case 1: Printf ("\n ..postponing Nobi.. ..fat.."); break; case 2: Printf ("[Minamotoshizuka] \n"); break; case 3: Printf ("Great physical strength [dentakeshi] \n"); break; case 4: Printf ("Bone river [sune] husband \n"); break; default: Printf ("The person of such a number is \n that not is"); break; } return 0; }
When the number of 1-4 is input, the corresponding name is displayed.
10 Input data
There is no person of such a number.
The following see the program that displays sex by using the list of names ahead by thinking.
Only it is to rewrite the character string, this is very easy.
The following program to rewrite and is making of the character string.
Though it becomes a familiar review method again when you see this program#include <stdio.h> int main (void) { int no; scanf("%d",&no); switch (no) { case 1: Printf ("Man \n"); break; case 2: Printf ("Woman \n"); break; case 3: Printf ("Man \n"); break; case 4: Printf ("Man \n"); break; default: Printf ("The person of such a number is \n that not is"); break; } return 0; }
Actually, case can use plurals by continuing it.
Because in case, there is only a meaning that shows the place that jumps by the switch sentence
There is no influence in the content of execution even if two or more case ties.
The following program It is the one that case of 1, 3, and 4 tied.
In this program When 1, 3, and 4 are input#include <stdio.h> int main (void) { int no; scanf("%d",&no); switch (no) { case 1: case 3: case 4: Printf ("Man \n"); break; case 2: Printf ("Woman \n"); break; default: Printf ("The person of such a number is \n that not is"); break; } return 0; }
[ It is ..forgetting break sentence.. ]
Though it ties case if the break sentence is excluded like this example
When the break sentence is forgotten if it says oppositely, unrelated case is connected.
The break sentence carelessly for remembrance' sake when you do not want to tie.
When you use switch sentence- case as understood from having seen up to now
In switch sentence- case, being able to put on case is only an integral value.
The real number, the variable, and the conditional expression, etc. cannot be put.
In a word, the variable is compared mutually like the if sentence, and the comparison of the magnitude correlations etc. are impossible.
Switch sentence- case can be used only to compare the variable and the integral value.
There is no method except that a complex judgment is necessary ..the use of the if sentence...