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

Iterative sentence

Repetitive action
Computers can repeat the same thing as many times as needed.
If needed, it can be repeated as many times as tens of thousands or even hundreds of millions.

Repetition can be either a fixed number of repetitions or an indefinite number of repetitions.
In C, for loops are used for definite iterations.
"The for loop is used in the following format:"

for loop
int i;
for (i = 1; i <= 繰り返し回数; i++) {
    繰り返す文;
}

This 'i' is an integer variable used to count the number of iterations.
Of course, this `i` needs to be declared before using it in the for loop.
The following program is an example of using a for loop to display a message 10 times.

Source code
#include <stdio.h>

int main(void)
{
    int i;
    
    for (i = 1; i <= 10; i++) {
        printf("メッセージ\n");
    }

    return 0;
}

The output of this program will be as follows:

Execution results
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ
メッセージ

When I counted, I found it appeared 10 times.
Count display
I learned that using a for loop allows you to repeat a process.
We refer to the variables used at this time as count variables or loop variables.
The counter variable doesn't have to be 'i', it can be anything you like.
In C, it's conventional to use `i`.

The value of the repetition count can be known at any time by referencing the variable i.
The following program is an example that displays the number of repetitions.

Source code
#include <stdio.h>

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

The output of this program will be as follows:

Execution results
01 回目
02 回目
03 回目
04 回目
05 回目
06 回目
07 回目
08 回目
09 回目
10 回目

Looking at the results, it's clear it appears 10 times.
a vast number of
You might have thought it was fine to write the printf statement ten times if you were only displaying it ten times.
However, in reality, the number of repetitions can be specified virtually without limit.
Computers don't get tired like humans do.Therefore, you can specify it, no matter how frightening the number of times.
まあ、本当に限界を超える無茶をさせると、熱暴走してしまうのですが・・・

The following program is a version with a loop count of 9999.

Source code
#include <stdio.h>

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

The output of this program will be as follows:

Execution results
0001 回目
0002 回目
0003 回目
0004 回目
0005 回目
0006 回目
0007 回目
0008 回目
0009 回目
0010 回目
0011 回目
0012 回目
0013 回目
0014 回目


~since省略~

Modern computers are so powerful that they won't even flinch with around 9,999 iterations.
You can run computers as much as you want, whether it's millions, billions, trillions, or even more.


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