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

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

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