MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us

Automatic version identification


It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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.

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++) {

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

source code
 #include <stdio.h>

int main(void)
    for (;;) {

    return 0;

The result of executing this program will be as follows

Execution Result
~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.

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.

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

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20


Open the 💬 comment submission box