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.
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.
The result of executing this program is as follows
If you count, you will see that it is displayed 10 times.
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
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 following program is an example of displaying the number of iterations.
The result of executing this program is as follows
The results show that it is displayed 10 times with great success.
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
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.
The result of executing this program is as follows
Modern computers are so powerful that 9999 or so repetitions will not freak them out.
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 ~~~.
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.