C语言控制语句与变量作用域详解
1. for语句
for语句允许程序员指定代码块的执行次数。其一般形式为:
for (initial-statement; condition; iteration-statement)
body-statement;
这等价于:
initial-statement;
while (condition) {
body-statement;
iteration-statement;
}
下面通过两个示例来对比while循环和for循环实现相同功能的代码:
- 示例1:使用while循环累加五个数
#include <stdio.h>
int total; /* total of all the numbers */
int current; /* current value from the user */
int counter; /* while loop counter */
char line[80]; /* Line from keyboard */
int main() {
total = 0;
counter = 0;
while (counter < 5) {
printf("Number? ");
fgets(line
超级会员免费看
订阅专栏 解锁全文
921

被折叠的 条评论
为什么被折叠?



