learn through suffering C language learn through suffering 
C 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 <= Loop Count; i++) {
    Loop Sentence;
}

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("Message\n");
    }

    return 0;
}

The output of this program will be as follows:

Results
Message
Message
Message
Message
Message
Message
Message
Message
Message
Message

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 time\n", i);
    }
    
    return 0;
}

The output of this program will be as follows:

Results
01 time
02 time
03 time
04 time
05 time
06 time
07 time
08 time
09 time
10 time

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 time\n", i);
    }
    
    return 0;
}

The output of this program will be as follows:

Results
0001 time
0002 time
0003 time
0004 time
0005 time
0006 time
0007 time
0008 time
0009 time
0010 time
0011 time
0012 time
0013 time
0014 time


~Omitted hereafter~

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

Loading comment system...