※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Divide according to the number.   

  1. ..clause 1..: Processing to make them correspond to number
  2. ..clause 2..: Processing when not applying
  3. ..clause 3..: Bring similar processing together.
  4. ..clause 4..: Weakness of judgment

[1] Processing to make them correspond to number

The judgment of any condition is also already possible by the method of the explanation before.
However, know the convenient judgment sentence prepared in another one and C language.

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
Think about the program that displays the corresponding name when you input the number.
This is easily feasible in the above-mentioned else-if sentence.

 #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;
}
It is not so beautiful in this method though is of course good continue else-if either.
It is possible to write beautifully by using switch (switch) sentence- case (case).
The usage of switch sentence- case is as follows.
  
Case  
numerical value:
	Executable statement;
	break; 
Case numerical value:
	Executable statement;
	break; 
}
The switch sentence jumps to case of the same value as the value of the specified conditional expression.
When the sentence of case or less is executed at the jump destination, and break (hit) sentence is found
It slips out from among.
The following program is an example to have rewritten by using switch sentence- case.
 #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;
}
Correspondence with the number is comprehensible, and is more beautiful than ahead.
Of course, when the number is input, the corresponding name is displayed.
You may arrange two or more sentences none.

[ 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.

It returns to contents.


[2] Processing when not applying

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.

 #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;
}
The result when this program is executed and ten is input becomes as follows.

10   Input data
There is no person of such a number.
When the number of 1-4 is input, the corresponding name is displayed.

It returns to contents.


[3] Bring similar processing together.

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.

 #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;
}
Though it becomes a familiar review method again when you see this program
The character string of "Man" is used as many as three times, and it is very useless.
Does not it not manage to be finished this by one time?

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.

 #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;
}
In this program When 1, 3, and 4 are input
Display "Man" both.

[ 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.

It returns to contents.


[4] Weakness of judgment

When you use switch sentence- case as understood from having seen up to now
The program that diverges to the multiway can be easily written.
However, the judgment is very weak to switch sentence- case actually.

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...

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.