C language learned by suffering
C language learned by suffering
Loop Operation Mechanism
Initialization and Conditions
In the previous section, we have seen that iterations (loops) can be realized with the for statement.
This section describes in detail how the for statement works.
A more concrete use of the for statement is as follows.
Initialization is a statement for initializing a count variable.
Expressions written here are executed only once at the beginning.
A conditional expression is a statement that sets the condition for the end of the loop.
As long as the value of the expression written here is true, the repeating statement continues to execute.
Update is a statement for updating the count variable.
The expression written here is executed after the statement that repeats is executed.
Based on this, let us examine the behavior of the program created in the previous section.
Here, the initialization expression is i=1.
This expression is executed only once at the beginning, so i is 1 at the start of the loop.
Next, the conditional expressions are compared.
At this stage, the value of i remains 1, so the result for i<=10 is true and
As a result, the loop will still continue to execute.
Next, repeat statements are executed. Here the printf statement is executed.
Next, an update expression is executed.
The value of i was previously 1, but since this update expression is i++, the value of i is increased by 1 to 2.
If you have forgotten the ++ operator, please look at Chapter 5, Section 1, Paragraph 6 to remind you.
This execution of the conditional expression, repeat statement, update, and so on, is repeated over and over again.
When i reaches 11, the condition i<=10 becomes false and the loop is broken.
In this way, by changing the value of the count variable and repeating until the condition becomes false
The loop process is performed a fixed number of times.
In a for statement, any expression can be placed in the initialization, conditional expression, and update sections.
It can be reduced by one each time, or it can be an unrelated expression.
However, to realize a loop with a fixed number of times, an expression like the one in the previous section is the easiest to use.
This section describes in detail how the for statement works.
A more concrete use of the for statement is as follows.
for statement
for (initialization; conditional expression; update) {
Repeating statement;
}
Initialization is a statement for initializing a count variable.
Expressions written here are executed only once at the beginning.
A conditional expression is a statement that sets the condition for the end of the loop.
As long as the value of the expression written here is true, the repeating statement continues to execute.
Update is a statement for updating the count variable.
The expression written here is executed after the statement that repeats is executed.
Based on this, let us examine the behavior of the program created in the previous section.
source code
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++) {
printf("message\n");
}
return 0;
}
Here, the initialization expression is i=1.
This expression is executed only once at the beginning, so i is 1 at the start of the loop.
Next, the conditional expressions are compared.
At this stage, the value of i remains 1, so the result for i<=10 is true and
As a result, the loop will still continue to execute.
Next, repeat statements are executed. Here the printf statement is executed.
Next, an update expression is executed.
The value of i was previously 1, but since this update expression is i++, the value of i is increased by 1 to 2.
If you have forgotten the ++ operator, please look at Chapter 5, Section 1, Paragraph 6 to remind you.
This execution of the conditional expression, repeat statement, update, and so on, is repeated over and over again.
When i reaches 11, the condition i<=10 becomes false and the loop is broken.
In this way, by changing the value of the count variable and repeating until the condition becomes false
The loop process is performed a fixed number of times.
In a for statement, any expression can be placed in the initialization, conditional expression, and update sections.
It can be reduced by one each time, or it can be an unrelated expression.
However, to realize a loop with a fixed number of times, an expression like the one in the previous section is the easiest to use.
Forever...
In a for statement, not only can you put any expression, but you do not have to put any expression.
The following program is a for statement without any expressions.
However, we recommend that you do not run this program.
The result of executing this program will be as follows
The program continues to display messages indefinitely.
It will never end unless the program is forced to terminate.
In the case of the 9999 times described in the previous section, it ends exactly at 9999 times, but
The above program does not really end forever.
This is because we omitted the conditional expression, so the part is always determined to be true, and
This is because the iteration was running forever.
A loop that executes indefinitely in this manner is called an infinite loop.
In fact, this infinite loop is a very widely used technique.
In the common applications that you use on a regular basis, the
The process of displaying the screen in response to a keystroke, mouse or touch input is called
The process repeats forever until the user exits the application.
As such, unless the user explicitly chooses to exit the program.
An infinite loop is used to prevent a program from ending.
The following program is a for statement without any expressions.
However, we recommend that you do not run this program.
source code
#include <stdio.h>
int main(void)
{
for (;;) {
printf("message\n");
}
return 0;
}
The result of executing this program will be as follows
Execution Result
Message
Message
Message
Message
Message
...
The
~Omitted after ~~.
Message
Message
Message
Message
...
The
~Omitted after ~~.
The program continues to display messages indefinitely.
It will never end unless the program is forced to terminate.
In the case of the 9999 times described in the previous section, it ends exactly at 9999 times, but
The above program does not really end forever.
This is because we omitted the conditional expression, so the part is always determined to be true, and
This is because the iteration was running forever.
A loop that executes indefinitely in this manner is called an infinite loop.
Keywords.
Infinite loop
A program that repeats the same action indefinitely.
In fact, this infinite loop is a very widely used technique.
In the common applications that you use on a regular basis, the
The process of displaying the screen in response to a keystroke, mouse or touch input is called
The process repeats forever until the user exits the application.
In other words, unlike the programs we have created so far, a typical application does not end on its own.
As such, unless the user explicitly chooses to exit the program.
An infinite loop is used to prevent a program from ending.
forced ejection
We have already explained that an infinite loop is used when you want to keep a program from ending unless the user explicitly chooses to exit the program.
However, the previous program did not provide a means to terminate the program in response to user input.
The for statement can be terminated by user input if the user input value is a specific value.
It is normal for a for statement to terminate when the conditional expression becomes false, but
In fact, it is possible to end a for statement on its own in the middle of the process.
To do so, use the break statement.
When a break statement is executed in a for statement, the for statement is forced to terminate and
The count variable remains at its current value.
The following program is an example of terminating a loop with a break statement.
The result of executing this program will be as follows
The conditional expression shows that it should not end until 10 displays are made, but it ends after 3 displays.
This is because the third time the value of i becomes 3, the break statement following the if statement is executed.
Since a break statement can be used in conjunction with a for statement condition, the
To end by user input, an error occurred in the middle of the iteration, etc.
It can be used to terminate before the number of iterations is complete.
However, the previous program did not provide a means to terminate the program in response to user input.
The for statement can be terminated by user input if the user input value is a specific value.
It is normal for a for statement to terminate when the conditional expression becomes false, but
In fact, it is possible to end a for statement on its own in the middle of the process.
To do so, use the break statement.
When a break statement is executed in a for statement, the for statement is forced to terminate and
The count variable remains at its current value.
The following program is an example of terminating a loop with a break statement.
source code
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
if (i == 3) break; /* exit the loop */
}
return 0;
}
The result of executing this program will be as follows
Execution Result
1
2
3
2
3
The conditional expression shows that it should not end until 10 displays are made, but it ends after 3 displays.
This is because the third time the value of i becomes 3, the break statement following the if statement is executed.
Since a break statement can be used in conjunction with a for statement condition, the
To end by user input, an error occurred in the middle of the iteration, etc.
It can be used to terminate before the number of iterations is complete.
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 better than any book on the market.