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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

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