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

推荐订阅源

D
DataBreaches.Net
SecWiki News
SecWiki News
博客园_首页
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
P
Palo Alto Networks Blog
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
WordPress大学
WordPress大学
NISL@THU
NISL@THU
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
Martin Fowler
Martin Fowler
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
小众软件
小众软件
I
Intezer
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
O
OpenAI News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V2EX - 技术
V2EX - 技术
Stack Overflow Blog
Stack Overflow Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 最新话题
博客园 - Franky
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
P
Proofpoint News Feed
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Full Disclosure
Security Latest
Security Latest

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