Home >Backend Development >C#.Net Tutorial >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:
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 = 5050The 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:
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!