while 语句中的 0
我有一个问题,请帮助我。我在网页上读过一些关于 do while 语句的内容,不同的是,在 while 中写的是 0,不是布尔条件
do{
// do some instruction
}while(condition );
是清楚可以理解的,但是这个
do
{
//again some instruction
}while(0);
我无法猜测它是什么意思做吗?它是否等同于:在(假)时做某事?或者可能是无限循环?请帮助我
i have one question and please help me.i have read on web page somthing about do while statement,different is that ,in while there is written 0,not boolean condition
do{
// do some instruction
}while(condition );
is clearly understandable,but this one
do
{
//again some instruction
}while(0);
i can't guess what does it do?is it equivalence it to this: do something while(false)?or maybe is it infinity loop?please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它只做某件事一次。它广泛使用在宏中对语句进行分组并使用法更自然(即需要结束分号)。
任何有价值的编译器都应该完全忽略 do .. while 并只运行所包含的语句。
It does something only once. It is widely used in macros to group statements and make the usage more natural (i.e. require ending semicolon).
Any compiler worth its salt should ignore the do .. while altogether and just run the enclosed statements.
虽然这个“循环”只执行一次,但可以这样使用它:
恕我直言,在大多数情况下,最好以更结构化的方式重写它,或者通过这种方式
或通过引入一个单独的函数来反映原始范围:
Though this "loop" is executed just once, one may use it this way:
IMHO, in most cases it is better to rewrite this in a more structured manner, either this way
or by introducing a separate function, reflecting the original scope:
它将把 0 视为 false,并且一旦到达 while(已测试 g++ 编译器),就会执行一次循环并中断。
It will treat 0 as false and one time loop will execute and break as soon as reached to while (g++ compiler tested).
do while 循环首先执行循环体中的所有指令,然后检查条件。因此,该循环仅执行循环体的指令一次。
相当于写
现代编译器足够聪明,可以优化这些东西!
The do while loop executes all the instructions in the body of the loop first and then checks for the condition. Hence, this loop just executes the instructions of the body of the loop once.
is equivalent to writing
The modern compilers are smart enough to optimize these things out!
从技术上讲,它使其运行一次,因为它在运行一次后进行检查(并检查失败)。
除此之外,它还用在宏中。例如:
会导致这样的情况:
这不是有效的语法,因为 else 不再是 if 语句的一部分。您可以做的是:
这将转换为 if 块中的单个语句:
Technically speaking, it makes it run once, as it checks (and fails the check) after it runs once.
Other than that, it's used in macros. For example:
would lead to this:
which is not valid syntax, as the else is not part of the if statement anymore. What you can do is:
which would translate into a single statement in the if block: