The for loop explained in the previous chapter was a loop that repeated a fixed number of times, but...
Conversely, you might want to know how many times to repeat it.
An interesting example of this is the calculation of
compound interest.
An elementary school student asked their parents."This month it's 1 yen, next month it's 2 yen, and the month after that it's 4 yen,""I want my allowance doubled from last month.""So, in which month will the amount paid by parents exceed 1 million yen?"
This calculation itself could also be determined mathematically, but...
Let's try calculating this time using a loop.
It can also be obtained using a for loop, but in cases like this,
A `while` loop is more appropriate than a `for` loop.
While statements are generally used in the following format.
As you can see, essentially,
"It's the same as specifying only the condition in a for loop."
"For loops allow for easy creation of constant-iteration loops because they allow for initialization and updates."
If you don't know how many times a loop needs to run, a while loop with only a condition is useful.
To perform the previous calculation using a while loop,
while the condition is less than 1 million yen, continue the loop.
You just need to double the money within the loop.
The following program is an example of how to calculate this.
#include <stdio.h>
int main(void)
{
double money = 1;
int month = 1;
while (money < 1000000) {
printf("%02d The Moon目 : %7.0f 円\n", month, money);
money *= 2;
month++;
}
printf("%02d The Moon目 に %7.0f 円となり、100万円を超える。\n", month, money);
return 0;
}
The results of this program's execution are as follows:
01 The Moon目 : 1 円
02 The Moon目 : 2 円
03 The Moon目 : 4 円
04 The Moon目 : 8 円
05 The Moon目 : 16 円
06 The Moon目 : 32 円
07 The Moon目 : 64 円
08 The Moon目 : 128 円
09 The Moon目 : 256 円
10 The Moon目 : 512 円
11 The Moon目 : 1024 円
12 The Moon目 : 2048 円
13 The Moon目 : 4096 円
14 The Moon目 : 8192 円
15 The Moon目 : 16384 円
16 The Moon目 : 32768 円
17 The Moon目 : 65536 円
18 The Moon目 : 131072 円
19 The Moon目 : 262144 円
20 The Moon目 : 524288 円
By month 21, it reached 1,048,576 yen, exceeding 1 million yen.
First, the reason we're calculating with doubles is...
"This is because the LSI-C86, a 16-bit compiler, cannot store millions in an integer type."
"It works fine with other compilers, so please adjust it to use int types yourselves."
int型よりもサイズの大きいlong typeを使えば扱えますが、
例題のプログラム程度では、long型を必要とすることはあまりないので、
long型についてのDescriptionは後回しといたします。
The key to this program lies in the conditional part.
I'm specifying money < 1000000 as a condition.
while the condition is true, that is, while the money is less than 10 million yen,
Keep executing the loop statement money*=2.
And the loop will end when the money exceeds 1 million yen.
And the final printf statement will display when it exceeds 1 million yen.
Like this, whether in a for loop or a while loop,
They both share the characteristic of iterating while changing a variable specified by a condition.
Otherwise, the conditions will remain the same, resulting in an infinite loop.
As explained in the preceding section, the condition in a while loop is the same as the condition in a for loop.
Conversely, it could be said that a for loop is an extension of a while loop.
Actually, these two can be used in similar ways.
"When using a while loop exactly like a for loop, you would do it like this:"
Initialization;
while (条件式) {
繰り返す文;
更新;
}
This way, you can achieve a fixed number of loops even with a while statement.
Conversely, when using a for loop like a while loop,
It's just a matter of omitting the initialization and update statements.
However, by convention, for loops are typically used for a fixed number of iterations.
While loops are most often used for indefinite loops.
These two sentences look very similar, but please use them carefully.