For loops for循环
For loops in C are straightforward(简单的). They supply(提供) the ability to create a loop - a code block that runs multiple times(多次). For loops require an iterator variable(迭代变量), usually notated(标记) as i
.
For loops give the following functionality(功能):
-
Initialize(初始化) the iterator variable using an initial value
-
Check if(是否) the iterator has reached(达到) its final value
-
Increases the iterator
For example, if we wish to iterate on a block for 10 times, we write:
int i;
for (i = 0; i < 10; i++) {printf("%d\n", i);
}
This block will print the numbers 0 through 9 (10 numbers in total 总共).
For loops can iterate(迭代) on array values. For example, if we would want to sum all the values of an array, we would use the iterator i
as the array index:
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
int i;for (i = 0; i < 10; i++) {sum += array[i];
}/* sum now contains a[0] + a[1] + ... + a[9] */
printf("Sum of the array is %d\n", sum);
Exercise
Calculate(计算 )the factorial 阶乘 (multiplication of all items array[0]
to array[9]
, inclusive), of the variable array
.
原:
#include <stdio.h>int main() {int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };int factorial = 1;int i;/* calculate the factorial using a for loop here*/printf("10! is %d.\n", factorial);
}
改:
#include <stdio.h>int main() {int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };int factorial = 1;int i;/* calculate the factorial using a for loop here*/for(i = 0; i<10;i++){factorial *=array[i];}printf("10! is %d.\n", factorial);
}