Home  >  Article  >  Backend Development  >  What is the usage of for in C language?

What is the usage of for in C language?

coldplay.xixi
coldplay.xixiOriginal
2020-06-24 14:07:599783browse

What is the usage of for in C language?

##The usage of for in C language is:

The general form of for loop statement is:

for (表达式1; 表达式2; 表达式3)
{
        语句;
}

First of all, two things should be emphasized Points:

1) Expression 1, expression 2 and expression 3 are separated by semicolons; never write them as commas.

2) Never add a semicolon after for (expression 1; expression 2; expression 3). Many novices will make this mistake - they can't help but add a semicolon after it.

Because the for loop can only control one statement after it, and in C language, the semicolon is also a statement - an empty statement. So if you add a semicolon at the end, the for loop can only control this semicolon, and the statements in the braces below do not belong to the for loop.

Let’s take a look at its execution process:

  • Solve expression 1.

  • Solve expression 2. If its value is true, execute the embedded statement specified in the for statement, and then execute step 3; if the value of expression 2 is false, end the loop and go to step 5.

  • Solve expression 3.

  • Go back to step 2 above and continue.

  • The loop ends and the statement below the for statement is executed.

It can be seen from this execution process that "expression 1" is only executed once, and the loop is between "expression 2", "expression 3" and "embedded statement" carried out in between.

The simplest form of the for statement is:

for (循环变量赋初值; 循环条件; 循环变量增值)
{
    语句;
}

Let me write a program for you to find the sum of 1 2 3 4... 100.

# include <stdio.h>
int main(void)
{
    int i;
    int sum = 0;  //sum的英文意思是“总和”
    for (i=1; i<=100; ++i)  //++是自加的意思, ++i相当于i = i + 1
    {
        sum = sum + i;  /*等价于sum += i;但是不建议这么写, 因为sum = sum + i看起来更清楚、更舒服*/
    }
    printf("sum = %d\n", sum);
    return 0;
}

The output result is:

sum = 5050

The function of this program is to find the sum of 1 2 3 4... 100. If there is no loop, adding once requires one statement, adding 100 times requires 100 statements. Here it is added from 1 to 100. If it is added from 1 to 10000, it will require 10000 statements. But having a loop is very convenient. You can add as much as you want, just change one parameter. So circulation is important.

Let’s see how the above program is executed according to the execution process.

1. First define a loop variable i. You don't need to assign an initial value to it when defining it. You can also assign an initial value to it in the for loop. But as mentioned before, it is best to initialize the variable when you define it. If the value is uncertain, initialize it to 0. Therefore, in the program, you can also assign an initial value to i when defining it. Then "expression 1" in the for loop can be omitted, but the semicolon cannot be omitted.

In this case, step 1 will be skipped during execution and go directly to step 2, leaving everything else unchanged. Therefore, the program can also be written as follows:

# include <stdio.h>
int main(void)
{
    int i = 1;
    int sum = 0;
    for (; i<=100; ++i)
    {
        sum = sum + i;
    }
    printf("sum = %d\n", sum);
    return 0;
}

Of course, you can also add expression 1. In the worst case, you have to reassign the value.

2. Then define a variable sum used to store "sum", assign it an initial value of 0, and then enter the for loop:

  • First solve the expression 1, that is, assign an initial value to variable i, i=1; expression 1 is only executed this time, and will not be executed again.

  • Then solve expression 2. If 1<=100 is true, execute the inline statement in the for loop, that is, sum=0 1.

  • Then execute step 3, variable i increases by 1, that is, variable i changes from 1 to 2.

  • Then solve the expression 2, if 2<=100 is true, then execute the embedded statement in the for loop, sum=0 1 2.

  • Then perform step 3. The variable i increases by 1, that is, the variable i changes from 2 to 3.

  • Then solve expression 2, that is, if 3<=100 is true, execute the embedded statement in the for loop, sum=0 1 2 3.

  • ......

The loop continues like this until i equals 100, and expression 2 is solved, that is, 100<=100 is established. , then the embedded statement in the for loop is executed, sum=0 1 2 3 … 100.

Then perform step 3, the variable i increases by 1, that is, the variable i changes from 100 to 101. Then solve expression 2, that is, if 101<=100 is not true, end the loop and execute the statement below the for loop, which is printf.

The above is the execution process of this program. Regarding the code standardization issue of for statements, there are two points that I want to emphasize with you:

1) if, else, for, while, and do can only control one statement after it. If you want to control more than Statements must be enclosed in braces {}. However, based on code standardization, {} must be added to the execution statements after if, else, for, while, and no matter how many lines there are, even if there is only one line.

2) Keywords such as if, for, while, etc. should leave a space and then follow the left bracket (() to highlight the keyword.

In addition, there is another knowledge point to follow in the above program. Let me tell you: functionally speaking,

for(i=1; i<=100; i) can be written as for(i=1; i<101; i), And it is recommended that you try to use this writing method. In other words, the loop condition of the loop statement should be written as half open and half closed as much as possible, whether it is a for loop or a while loop.

for(i=1; i<101; ++i)实际上是 1≤i<101,是半开半闭的;而for(i=1; i<=100; ++i)实际上是 1≤i≤100,是全闭的。那么为什么建议使用半开半闭的呢?因为如果写成 i<=100 的话,那么每次判断的时候都要判断两次,即 i<100 和 i==100,而写成 i<101 的话每次只需要判断一次。

下面再给大家写一个程序,求 1 到 100 之间所有奇数的和。

# include <stdio.h>
int main(void)
{
    int i;
    int sum = 0 ;
    for (i=1; i<100; i+=2)  //i+=2;等价于i = i + 2;
    {
        sum = sum + i;
    }
    printf("sum = %d\n", sum);
    return 0;
}

输出结果是:

sum = 2500

推荐教程:《C视频教程

The above is the detailed content of What is the usage of for in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn