惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
GbyAI
GbyAI
博客园 - Franky
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
InfoQ
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
量子位
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
L
LINUX DO - 最新话题
Simon Willison's Weblog
Simon Willison's Weblog
爱范儿
爱范儿
博客园 - 聂微东
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
D
Docker

lzw-723's blog

raylib基础学习 | lzw-723's blog Lojban教程 - 逻辑语学习中常见专有名词列表 | lzw-723's blog Lojban基础教程 - 字母 | lzw-723's blog 扩充巴科斯范式(ABNF)初识 | lzw-723's blog 【译】5分钟入门Nim编程语言 | lzw-723's blog Cakewalk安装无反应的解决办法 | lzw-723's blog C语言教程 - while循环 | lzw-723's blog C++ FAQ | lzw-723's blog 《Moe Era》 - 时代潮流中不值一提的我 | lzw-723's blog
C语言教程 - for循环 | lzw-723's blog
2021-09-04 · via lzw-723's blog

2021-09-05 2 min read # C # 教程 # 翻译

C语言中的for循环非常简单。

Tutorial

C语言中的for循环非常简单。你能用它创建一个循环—一块运行多次的代码块。
for循环需要一个用来迭代的变量,通常命名为i

for循环能够做这些:

  • 用一个初始值初始化迭代器变量
  • 检查迭代变量是否达到最终值
  • 增加迭代变量的值

如果想运行代码块10次,可以这样写:

int i;
for (i = 0; i < 10; i++) {
    printf("%d\n", i);
}

这段代码会打印从0到9的数字。

for循环能够用来获取数组的每一个值。要计算一个数组所有值的和,可以这样使用i

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];
}

/* 求a[0]到a[9]的和 */
printf("Sum of the array is %d\n", sum);

Exercise

计算数组array的阶乘(从array[0]乘到array[9])。

Tutorial Code

#include <stdio.h>

int main() {
  int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int factorial = 1;
  int i;

  /* 在这里使用for循环计算阶乘*/

  printf("10! is %d.\n", factorial);
}

Expected Output

10! is 3628800.

Solution

#include <stdio.h>

int main() {
  int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int factorial = 1;

  int i;

  for(i=0;i<10;i++){
    factorial *= array[i];
  }

  printf("10! is %d.\n", factorial);
}
    • Tutorial
    • Exercise
    • Tutorial Code
    • Expected Output
    • Solution