C has three loop statements in total.
"It's about while loops, for loops, and do-while loops."
While and for loops have already been explained.
Actually, do-while loops are almost identical to while loops.
"do-while statements are generally used in the following format:"
do {
繰り返す文;
} while (条件式);
The meaning and behavior of a conditional statement are exactly the same as those of a while statement.
In other words, it repeats the execution of the loop statement as long as the condition is true.
Furthermore, as a difference in notation, in the case of do-while statements,
"A semicolon
; is required after the condition expression in a while statement."
The only difference between a do-while loop and a while loop is
The difference lies in whether the conditional statement is evaluated after the fact or in advance.
A while loop evaluates a condition before executing the statements within the loop.
However, the do-while loop evaluates the condition *after* the block of code it repeats has been executed.
Actually, it doesn't make that much of a difference.
In fact, a while loop and a do-while loop can sometimes produce the same result.
Therefore, do-while loops are not frequently used.
However, do-while loops have one significant advantage.
That's because a loop is guaranteed to execute at least once.
In the case of a while loop, if the condition is initially false,
It will end without the loop ever being executed.
With a do-while loop, the code block will always execute at least once.
The advantage of a do-while loop is that it guarantees at least one execution.
Above all, it really shines when it comes to
input validation.
For example, let's try creating a program to calculate the area of a circle.
The area of a circle is radius times radius times pi.
"Here, we're checking that the radius isn't a negative value, as that wouldn't make sense."
コンピュータの世界では、円周率を3.14159で計算します。
これは、その次の桁まで表すと、3.141592、となり、
9と2の間で切り捨てても、誤差が大きくならないからis.
Next, let's check the case without using loops.
#include <stdio.h>
int main(void)
{
int r;
double s;
printf("Radius?:");
scanf("%d", &r);
if (r < 0) {
printf("半径は負の値になりません。\n");
} else {
s = r * r * 3.14;
printf("面積は %f is.\n", s);
}
return 0;
}
"When running this program with an input of 8, the result is as follows:"
Radius?:8 入力した値
The area is 200.960000.
"The result of running this program with the input -8 is as follows."
Radius?:-8 入力した値
半径は負の値になりません。
While this technically checks out, it's a bit unfriendly.
If the input is incorrect, let's have them re-enter it.
The following program is an example of re-entrancy implemented with a while loop.
#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("面積は %f is.\n", s);
return 0;
}
“When running this program with the inputs -8, -5, and 8, the result is as follows.”
Radius?:-8 入力した値
Radius?:-5 入力した値
Radius?:8 入力した値
The area is 200.960000.
The retyping was flawlessly done.
However, the problem lies with the program this time.
Looking at the program, I can see that the scanf function is being used twice.
Writing the `scanf` function twice for the same data input is truly wasteful.
The following program is an example of eliminating this waste with a do-while loop.
#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("面積は %f is.\n", s);
return 0;
}
“When running this program with the inputs -8, -5, and 8, the result is as follows.”
Radius?:-8 入力した値
Radius?:-5 入力した値
Radius?:8 入力した値
The area is 200.960000.
"This time, you only need to write the scanf function once."
The advantage of the do-while loop, which guarantees execution at least once, is being utilized.