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


  Input check   

  1. ..clause 1..: Judgment and judgment ahead
  2. ..clause 2..: Input check

[1] Judgment and judgment ahead

Three loop statements in all are prepared in C language.
It is while sentence, for sentence, and do ([deyu;u])-while sentence.

The while sentence and the for sentence though it has already explained
Actually, the do-while sentence is almost the same as the while sentence.
In general, use the do-while sentence by the following writing.

 
Repeated  sentence;
} While (conditional expression);
The meaning of the conditional expression and operation are quite similar to the while sentence.
That is, repeat the execution of the sentence that the conditional expression repeats during the truth.
For the do-while sentence as a difference of writing
After () of the conditional expression of the while sentence;However. should

The only difference between the do-while sentence and the while sentence
It is a difference of whether it is a judgment whether the conditional expression is a post-judgment ahead.
The while sentence judges the conditional expression before executing the repeated sentence.
However, after executing the repeated sentence, the do-while sentence judges the conditional expression.

Actually, it doesn't become too big different it is possible to hold this.
Actually, the while sentence and the do-while sentence might become the same deals.
Therefore, the do-while sentence is not so used.

However, there is one major plus in the do-while sentence.
In it, the for statement is always a point of being executed once.

When the conditional expression is an imitation from beginning for the while sentence
The for statement : without executing the throw though it will be ended.
If it is do-while sentence, it is executed only once even if it is the lowest.

It returns to contents.


[2] Input check

The advantage of being executed : once about the do-while sentence.
Exercise your power at the input check even if you say very much.

For instance, make the program that calculates the area of a circle.
The area of a circle is radius x radius x pi.
Here, saying that the radius is a negative value checks it because it is amused.


[   Yen [shuuritsu] is how many   ].
In the world of computer, calculate pi by 3.14159.
This becomes 3.141592 as long as the following digit is shown.
It is because the error margin doesn't grow even if it rounds it down between 9 and 2.
The following are checks when the loop is not used.
 
#include <stdio.h> 

int main(void)  
{

	int r;  
	double s;  
	
	Printf ("Radius?");  
	scanf("%d",&r);  
	Radius..negative..value..become.\n");  
	} else { 
		s = r * r * 3.14;  
		Printf ("The area is %f.  \n" s);  
	}
	
	return 0;
}
This program is executed, and the result of inputting eight is as follows.

Radius?:8
The area 200.
This program is executed, and the result of inputting -8 is as follows.

Radius?:-8
The radius doesn't reach a negative value.
Even this is unkind for a moment though is made of one the respondent check.
It is usual to make them input it again if the input value is wrong.
The following program is an example of mounting the re-input by the while sentence.
 
#include <stdio.h> 

int main(void)  
{

	int r;  
	double s;  
	
	Printf ("Radius?");  
	scanf("%d",&r);  
	
	 Printf ("Radius?");
		scanf("%d",&r);  
	}
	
	s = r * r * 3.14;  
	Printf ("The area is %f.  \n" s);  
	
	return 0;
	}
This program is executed, and the result of inputting -8 and 5 and -8 is as follows.

Radius?:-8
Radius?:-5
Radius?:8
The area 200.
The re-input is splendidly done.
However, a next problem is on the program side.
When the program is seen, it is understood to use the scanf function twice.
It is exactly useless to write the scanf function twice though it is an input of the same data.
The following program is an example of losing this uselessness in the do-while sentence.
 
#include <stdio.h> 

int main(void)  
{

	int r;  
	double s;  

	 Printf ("Radius?");
		scanf("%d",&r);  
	} while (r < 0);  
	
	s = r * r * 3.14;  
	Printf ("The area is %f.  \n" s);  
	
	return 0;
}
This program is executed, and the result of inputting -8 and 5 and -8 is as follows.

Radius?:-8
Radius?:-5
Radius?:8
The area 200.
This time, the scanf function ends if it writes only once.
The advantage of the do-while sentence of being executed once is made the best use of without fail.

It returns to contents.


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