C language learned by suffering
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
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.
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.
The area of a circle is radius x radius x circumference.
Here, it is strange to say that the radius is a negative value, so we check it.
The next check is for when loops are not used.
The result of running this program and entering 8 is as follows
The result of running this program and entering -8 is as follows
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.
The results of running this program and entering -8, -5, and 8 are as follows
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.
The results of running this program and entering -8, -5, and 8 are as follows
This time, the scanf function only needs to be written once.
The advantage of the do~while statement, which is always executed once, is taken advantage of.
After all, it is a powerful tool for input checking.
For example, let's create a program to calculate the area of a circle.
The area of a circle is radius x radius x circumference.
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.
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;
printf("Radius? :");
scanf("%d", &r);
if (r < 0) {
printf("Radius cannot be negative. \n");
} 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
Radius? :8 Input value
The area is 200.960000.
The area is 200.960000.
The result of running this program and entering -8 is as follows
Execution Result
Radius ? :-8 Value entered
Radius cannot be negative.
Radius cannot be negative.
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;
printf("Radius? :");
scanf("%d", &r);
while (r < 0) {
printf("Radius? :");
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
Radius ? :-8 Value entered
Radius ? Value entered
Radius? :8 Value entered
The area is 200.960000.
Radius ? Value entered
Radius? :8 Value entered
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 {
printf("Radius? :");
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
Radius ? :-8 Value entered
Radius ? Value entered
Radius? :8 Value entered
The area is 200.960000.
Radius ? Value entered
Radius? :8 Value entered
The area is 200.960000.
This time, the scanf function only needs to be written once.
The advantage of the do~while statement, which is always executed once, is taken advantage of.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.