learn through suffering C language learn through suffering 
C 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.

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 statement
while (conditional expression) {
    Loop Sentence;
}

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 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("On the %02dth day of the month, it reached %7.0f yen, exceeding 1 million yen.\n", month, money);

    return 0;
}

The output of this program is as follows:

Results
Month 1: 1 yen
Month 2: 2 yen
Month 3: 4 yen
Month 4: 8 yen
Month 5: 16 yen
Month 6:   32 yen
Month 7:   64 yen
Month 8:   128 yen
Month 9:   256 yen
Month 10:   512 yen
Month 11:  1024 yen
Month 12:  ¥2048
13th Month:  ¥4096
14th Month:  ¥8192
15th Month:  ¥16384
16th Month:  ¥32768
17th Month:  ¥65,536
Month 18: ¥131,072
Month 19: ¥262,144
Month 20: ¥524,288
Month 21: ¥1,048,576, exceeding ¥1 million.

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
While you can handle larger values using the long type, which is larger than the int type,
programs like the example one rarely require the long type,
so we'll postpone the explanation of the long type for now.

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 statement
Initialization;
while (conditional expression) {
    Loop Sentence;
    Update;
}

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

Loading comment system...