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

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

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++ FAQ | lzw-723's blog 《Moe Era》 - 时代潮流中不值一提的我 | lzw-723's blog C语言教程 - for循环 | lzw-723's blog
C语言教程 - while循环 | lzw-723's blog
2021-11-02 · via lzw-723's blog

2021-11-02 2 min read # C # 教程 # 翻译

while循环与for循环很像,但功能更少。


Tutorial

while循环与for循环很像,但功能更少。只要条件为真while循环会一直执行代码块。例如下面的代码会执行十次:

int n = 0;
while (n < 10) {
    n++;
}

while循环会一直执行只要判断为真(即非零值):

while (1) {
   /* 做某事 */
}

循环指令

在C语言中有两个重要的循环指令在所有的循环类型起作用——breakcontinue指令。

在循环10次后break指令停止循环,尽管从条件来这个while循环判断永远不会结束:

int n = 0;
while (1) {
    n++;
    if (n == 10) {
        break;
    }
}

在下面的代码中,continue指令使printf命令被跳过,所以只有偶数被打印出来:

int n = 0;
while (n < 10) {
    n++;

    /* 检查n是否为奇数 */
    if (n % 2 == 1) {
        /* 回到while代码块的开头 */
        continue;
    }

    /* 只有当n是偶数时,才能执行到这行代码 */
    printf("The number %d is even.\n", n);
}

Exercise

array变量是一个10个数字组成的序列。在while循环中,你必须写两个if判断,
它们以如下方式改变循环的流程(不改变printf命令):

  • 如果当前数字小于5,不打印。
  • 如果当前数字大于10,不打印并停止循环。

请注意:如果不推进迭代器变量i并使用continue指令,你将陷入死循环。

Tutorial Code

#include <stdio.h>

int main() {
    int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
    int i = 0;

    while (i < 10) {
        /* 在这里写你的代码 */

        printf("%d\n", array[i]);
        i++;
    }

    return 0;
}

Expected Output

7
5
9
5

Solution

#include <stdio.h>

int main() {
    int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4};
    int i = 0;

    while (i < 10) {
        if(array[i] < 5){
            i++;
            continue;
        }

        if(array[i] > 10){
            break;
        }

        printf("%d\n", array[i]);
        i++;
    }

    return 0;
}

    • Tutorial
      • 循环指令
    • Exercise
    • Tutorial Code
    • Expected Output
    • Solution