C language learned by suffering
C language learned by suffering
Loop of unknown frequency
Loop to find the number of times
The for statement explained in the previous chapter was a statement that repeats a fixed number of times.
Conversely, you may want to know how many times to repeat the process.
An interesting example of this is the computation of mousetrap operations.
This calculation itself can be obtained mathematically.
In this case, we will use a loop to do the calculation.
You can also use a for statement to obtain the value, but in such a case, the
A while (while) statement is more suitable than a for statement.
While statements are generally written in the following manner.
As you can see from this, the bottom line is.
It is the same thing as a "for" statement where only a conditional expression is specified.
In a for statement, you can use initialization and update, so it's easy to write a loop that loops a constant number of times.
If the number of loops is not known, a while statement with only a conditional expression is useful.
To perform the previous calculation in a while statement, use the
Specify in the condition of the while statement that the iteration should continue as long as the amount is less than 1,000,000 yen, and then type
We can just double the money in the repeating statement.
The following program is an example of how this is calculated.
The result of executing this program will be as follows
First, let me tell you why I'm calculating with the double type.
This is because the LSI-C86, a 16-bit compiler, cannot store millions in int type.
Other compilers have no problem, so please try changing the type to int by yourself.
The point of this program is in the conditions section.
The condition is money<1000000.
The while statement will be executed while this condition is true, i.e., while the money is less than 1,000,000 yen.
Keep executing the repeating statement money*=2.
Then, when the money exceeds 1 million yen, the iteration ends.
Then, the final printf statement displays that the amount has exceeded 1 million yen.
As you can see, you can use either a for or a while statement.
The same is true in that the variable specified in the condition is repeated while changing it.
Otherwise, the condition remains the same, resulting in an infinite loop.
Conversely, you may want to know how many times to repeat the process.
An interesting example of this is the computation of mousetrap operations.
Execution Result
An elementary school student asked his parents for a favor. Now, how many months will it be before the amount the parents have to pay exceeds one million yen?
This calculation itself can be obtained mathematically.
In this case, we will use a loop to do the calculation.
You can also use a for statement to obtain the value, but in such a case, the
A while (while) statement is more suitable than a for statement.
While statements are generally written in the following manner.
while statement
while (conditional expression) {
Repeating statement;
}
As you can see from this, the bottom line is.
It is the same thing as a "for" statement where only a conditional expression is specified.
In a for statement, you can use initialization and update, so it's easy to write a loop that loops a constant number of times.
If the number of loops is not known, a while statement with only a conditional expression is useful.
To perform the previous calculation in a while statement, use the
Specify in the condition of the while statement that the iteration should continue as long as the amount is less than 1,000,000 yen, and then type
We can just double the money in the repeating statement.
The following program is an example of how this is calculated.
while statement
#include <stdio.h>
int main(void)
{
Double money = 1;
int month = 1;
while (money < 1000000) {
printf("%02d Month : %7.0f Yen\n", month, money);
money *= 2;
month++;
}
printf("%7.0f yen in the %02d month and exceeds 1,000,000 yen. \n", month, money);
return 0;
}
The result of executing this program will be as follows
Execution Result
01th month : 1 yen
02 Month 1 : 2 yen
03 Month : 4 yen
Month 04 : 8 yen
05th month : 16 yen
06th month : 32 yen
07th month : 64 yen
08 Month : 128 yen
09th month : 256 yen
10th month : 512 yen
November : 1024 yen
12th month : 2048 yen
13th month : 4096 yen
14th month : 8192 yen
15th month : 16384 yen
16th month : 32768 yen
17 Month : 65536 yen
18 Month 1 : 131072 yen
Month 19 : 262144 yen
20th month : 524288 yen
In the 21st month, the amount is 1048576 yen and exceeds 1,000,000 yen.
02 Month 1 : 2 yen
03 Month : 4 yen
Month 04 : 8 yen
05th month : 16 yen
06th month : 32 yen
07th month : 64 yen
08 Month : 128 yen
09th month : 256 yen
10th month : 512 yen
November : 1024 yen
12th month : 2048 yen
13th month : 4096 yen
14th month : 8192 yen
15th month : 16384 yen
16th month : 32768 yen
17 Month : 65536 yen
18 Month 1 : 131072 yen
Month 19 : 262144 yen
20th month : 524288 yen
In the 21st month, the amount is 1048576 yen and exceeds 1,000,000 yen.
First, let me tell you why I'm calculating with the double type.
This is because the LSI-C86, a 16-bit compiler, cannot store millions in int type.
Other compilers have no problem, so please try changing the type to int by yourself.
long(long) type
Although it can be handled by using the long (long) type, which is larger than the int type
The example program does not require the long type very often.
We will explain the long type later.
The example program does not require the long type very often.
We will explain the long type later.
The point of this program is in the conditions section.
The condition is money<1000000.
The while statement will be executed while this condition is true, i.e., while the money is less than 1,000,000 yen.
Keep executing the repeating statement money*=2.
Then, when the money exceeds 1 million yen, the iteration ends.
Then, the final printf statement displays that the amount has exceeded 1 million yen.
As you can see, you can use either a for or a while statement.
The same is true in that the variable specified in the condition is repeated while changing it.
Otherwise, the condition remains the same, resulting in an infinite loop.
Compatibility with for statement
As explained in the previous section, a while statement is the same as just a conditional expression in a for statement.
Conversely, the for statement is an extension of the while statement.
In fact, the two can be used in the same way.
To use a while statement in exactly the same way as a for statement, do the following
In this way, a constant loop can be achieved even with a while statement.
Conversely, if you use a for statement like a while statement
Simply omit the initialization and update statements.
The while statement is most often used for loops that are not.
Although the two statements are very similar, they should be used interchangeably.
Conversely, the for statement is an extension of the while statement.
In fact, the two can be used in the same way.
To use a while statement in exactly the same way as a for statement, do the following
while statement
Initialization;
while (conditional expression) {
Repeating statement;
Update ;
}
In this way, a constant loop can be achieved even with a while statement.
Conversely, if you use a for statement like a while statement
Simply omit the initialization and update statements.
However, as a convention, the for statement is used to loop a constant number of times, and the
The while statement is most often used for loops that are not.
Although the two statements are very similar, they should be used interchangeably.
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.