MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Unspecified loop

Counting loop
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.

Execution results
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.

while loop
while (条件式) {
    繰り返す文;
}

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.

while loop
#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:

Execution results
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."

long type
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.
Compatibility with for loops
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:"

while loop
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.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19