Skip to content

Commit d9f5a5a

Browse files
lzw-723ronreiter
authored andcommitted
finished the for loops.
1 parent 674aa8f commit d9f5a5a

File tree

3 files changed

+78
-78
lines changed

3 files changed

+78
-78
lines changed

tutorials/learn-c.org/zh/For loops.md

Lines changed: 0 additions & 77 deletions
This file was deleted.

tutorials/learn-c.org/zh/Welcome.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ learn-c.org仍在建设中——如果你想贡献教程,请点击下方的`
1616
- [[多维数组]]
1717
- [[条件语句]]
1818
- [[字符串]]
19-
- [[For loops]]
19+
- [[for循环]]
2020
- [[While loops]]
2121
- [[Functions]]
2222
- [[Static]]

tutorials/learn-c.org/zh/for循环.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Tutorial
2+
--------
3+
4+
C语言中的for循环非常简单。你能用它创建一个循环 - 一块运行多次的代码块。
5+
for循环需要一个用来迭代的变量,通常命名为`i`
6+
7+
for循环能够做这些:
8+
9+
* 用一个初始值初始化迭代器变量
10+
* 检查迭代变量是否达到最终值
11+
* 增加迭代变量的值
12+
13+
如果想运行代码块10次,可以这样写:
14+
15+
int i;
16+
for (i = 0; i < 10; i++) {
17+
printf("%d\n", i);
18+
}
19+
20+
这段代码会打印从0到9的数字。
21+
22+
for循环能够用来获取数组的每一个值。要计算一个数组所有值的和,可以这样使用`i`
23+
24+
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
25+
int sum = 0;
26+
int i;
27+
28+
for (i = 0; i < 10; i++) {
29+
sum += array[i];
30+
}
31+
32+
/* 求a[0]到a[9]的和 */
33+
printf("Sum of the array is %d\n", sum);
34+
35+
Exercise
36+
--------
37+
38+
计算数组array的阶乘(从array[0]乘到array[9])。
39+
40+
41+
Tutorial Code
42+
-------------
43+
44+
#include <stdio.h>
45+
46+
int main() {
47+
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
48+
int factorial = 1;
49+
int i;
50+
51+
/* 在这里使用for循环计算阶乘*/
52+
53+
printf("10! is %d.\n", factorial);
54+
}
55+
56+
Expected Output
57+
---------------
58+
59+
10! is 3628800.
60+
61+
Solution
62+
--------
63+
64+
#include <stdio.h>
65+
66+
int main() {
67+
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
68+
int factorial = 1;
69+
70+
int i;
71+
72+
for(i=0;i<10;i++){
73+
factorial *= array[i];
74+
}
75+
76+
printf("10! is %d.\n", factorial);
77+
}

0 commit comments

Comments
 (0)