MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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.

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.

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 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

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box