﻿ Input check - C language learned painstakingly
C language learned by suffering

### input check

##### posterior and antecedent judgment
The C language provides a total of three loop statements.
These are the while statement, the for statement, and the do (du)-while statement.

We have already discussed the while and for statements.
In fact, the do~while statement is almost identical to the while statement.
A do~while statement is generally written in the following manner

do to while statement
`````` do {
Repeating statement;
} while (conditional expression);``````

The meaning and behavior of the conditional expression is exactly the same as the while statement.
That is, the execution of the repeating statement is repeated while the conditional expression is true.
One difference in the way it is written is that in the case of a do-to-while statement, the
The ; is required after the () in the conditional expression of the while statement.

The only difference between a do~while statement and a while statement is that a
The difference is whether the conditional expression is a post-determination or a predetermination.
The while statement determines the conditional expression before executing the repeating statement.
The do~while statement, however, determines the conditional expression after the repeating statement is executed.

Actually, this alone does not make much difference.
In fact, the same result can be obtained with either a while or a do~while statement.
For this reason, do~while statements are not often used.

However, the do~while statement has one major advantage.
That is, a repeating statement is always executed at least once.

In the case of a while statement, if the conditional expression is false from the beginning, then
The iterative statement would end up not being executed once.
With a do~while statement, it will be executed at least once.
##### input check
The advantage of the do~while statement, which is always executed once, is that
After all, it is a powerful tool for input checking.

For example, let's create a program to calculate the area of a circle.
Here, it is strange to say that the radius is a negative value, so we check it.

How many is pi
In the computer world, pi is calculated as 3.14159.
This means that the next digit is 3.141592, and the next digit is 3.141592.
This is because truncating between 9 and 2 does not result in a large error.

The next check is for when loops are not used.

do to while statement
`````` #include <stdio.h>

int main(void)
{

int r;
double s;

scanf("%d", &r);

if (r < 0) {
} else {
s = r * r * 3.14;
printf("Area is %f. \n", s);
}

return 0;
}``````

The result of running this program and entering 8 is as follows

Execution Result
The area is 200.960000.

The result of running this program and entering -8 is as follows

Execution Result

This is still a check, but it is a bit unfriendly.
If the input value is incorrect, let them re-enter it.
The following program is an example of implementing reentry with a while statement.

do to while statement
`````` #include <stdio.h>

int main(void)
{

int r;
double s;

scanf("%d", &r);

while (r < 0) {
scanf("%d", &r);
}

s = r * r * 3.14;
printf("Area is %f. \n", s);

return 0;
}``````

The results of running this program and entering -8, -5, and 8 are as follows

Execution Result
The area is 200.960000.

The re-entry has been done beautifully.

This time, however, the problem lies on the program side.
If you look at the program, you will see that the scanf function is used twice.
Writing the scanf function twice for the same data input is just wasteful.
The following program is an example of eliminating this waste with a do~while statement.

do to while statement
`````` #include <stdio.h>

int main(void)
{

int r;
double s;

do {
scanf("%d", &r);
} while (r < 0);

s = r * r * 3.14;
printf("Area is %f. \n", s);

return 0;
}``````

The results of running this program and entering -8, -5, and 8 are as follows

Execution Result