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.
The meaning of the conditional expression and operation are quite similar to the while sentence.Repeated sentence; } While (conditional expression);
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.
The advantage of being executed : once about the do-while sentence.
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.
The following are checks when the loop is not used.
[ 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.
This program is executed, and the result of inputting eight is as follows.#include <stdio.h> int main(void) { int r; double s; Printf ("Radius?"); scanf("%d",&r); Radius..negative..value..become. FONT>\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 -8 is as follows.
Radius?:8
The area 200.
Even this is unkind for a moment though is made of one the respondent check.
Radius?:-8
The radius doesn't reach a negative value.
This program is executed, and the result of inputting -8 and 5 and -8 is as follows.#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; }
The re-input is splendidly done.
Radius?:-8
Radius?:-5
Radius?:8
The area 200.
This program is executed, and the result of inputting -8 and 5 and -8 is as follows.#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 time, the scanf function ends if it writes only once.
Radius?:-8
Radius?:-5
Radius?:8
The area 200.