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;
}
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;
}
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;
}
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.