什么用于 (;;) 和 while();在 C 中的平均值 [英] What for (;;) and while(); mean in C

查看:20
本文介绍了什么用于 (;;) 和 while();在 C 中的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一些示例代码,我看到有人这样做了

I am looking at some example codes and I saw someone did this

for (;;) {
// ...
}

这等价于 while(1) { } 吗?

while(condition); 做了什么?我不明白用 ';' 代替 {}

And what does while(condition); do? I don't get the reason behind putting ';' instead of {}

推荐答案

while(condition);做?我不明白放置 ';' 的原因而不是 {}

And what does while(condition); do? I don't get the reason behind putting ';' instead of {}

好吧,您的问题是,如果在 while 条件之后放置或不放置分号会发生什么?计算机将分号识别为空语句.

Well, your question is what happens if you put or you do not put a semicolon after that while condition? The computer identifies the semicolon as an empty statement.

试试这个:

#include<stdio.h>

int main(void){
    int a = 5, b = 10;

    if (a < b){
        printf("True");
    }


    while (a < b); /* infinite loop */
        printf("This print will never execute
");

    return 0;
}

这篇关于什么用于 (;;) 和 while();在 C 中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆