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

Sentences that repeat themselves

iterative operation
Computers can do the same thing over and over again.
If necessary, we can have it repeated tens or hundreds of thousands or even hundreds of millions of times.

There are two types of repetitions: repetitions with a fixed number of times and repetitions with an unknown number of times.
In C, a for statement is used for a fixed number of iterations.
The for statement is used in the following writing style.

for statement
 int i;
for (i = 1; i <= number of iterations; i++) {
    Repeating statement ;
}

This i is an integer variable and is used to count the number of iterations.
Naturally, this i must be declared before a for statement is used.
The following program is an example of using a for statement to display a message 10 times.

source code
 #include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i <= 10; i++) {
        printf("message\n");
    }

    return 0;
}

The result of executing this program is as follows

Execution Result
Message
Message
Message
Message
Message
Message
Message
Message
Message
Message

If you count, you will see that it is displayed 10 times.
Display of number of times
We have seen that the for statement can be used to perform repetition.
The variables used in this case are called counting variables or loop variables.
The count variable does not have to be i, but can be anything.
In C, it is customary to use i.

The value of the number of iterations can be known at any time by reference to the variable i.
The following program is an example of displaying the number of iterations.

Source Code
 #include <stdio.h>

int main(void)
{
    int i;
    for (i = 1; i <= 10; i++) {
        printf("%02d times\n", i);
    }

    return 0;
}

The result of executing this program is as follows

Execution Result
01th time
02nd time
03rd
04th
05th
06th
07th
08th
09th
Tenth

The results show that it is displayed 10 times with great success.
Huge number of times
You might have thought that writing 10 printf statements would not be a problem if all you had to do was display the data 10 times.
In fact, however, the number of repetitions can be specified as virtually unlimited.
Computers, unlike humans, do not tire. Thus, any number of times, no matter how formidable, can be specified.
Well, if you really push them beyond their limits of recklessness, they will go into thermal runaway...

The following program is a program that tries to set the number of iterations to 9999.

source code
 #include <stdio.h>

int main(void)
{
    int i;
    for (i = 1; i <= 9999; i++) {
        printf("%04d times\n", i);
    }

    return 0;
}

The result of executing this program is as follows

Execution Result
0001th time
0002nd time
0003rd
0004th
0005th
0006th
0007th
0008th
0009th
0010th
0011th
0012th
0013th
0014th
...
...
~Omitted after ~~~.

Modern computers are so powerful that 9999 or so repetitions will not freak them out.



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