do-while(false)的优点是什么? [英] What is the advantage of a do-while(false)?

查看:171
本文介绍了do-while(false)的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看由其他员工处理的一些代码时,我看到很多代码都写在:

When looking through some code that was handled by another employee, I see a lot of code written in:

do{
    ...
}while(false);

这提供了什么优势(如果有的话)?

What advantage (if any) does this provide?

以下是代码中发生的更多骨架:

Here is more of a skeleton that is happening in the code:

try{
    do{
        // Set some variables

        for(...) {
            if(...) break;
            // Do some more stuff
            if(...) break;
            // Do some more stuff
        }
    }while(false);
}catch(Exception e) { 
    // Exception handling 
}

更新:


C ++版本:

伪造的循环是否常见?


推荐答案

也许这样做是为了能够随时跳出循环,例如:

Maybe it was done to be able to jump out of the "loop" at any time, e.g:

do
{
    ...
    if (somethingIsWrong) break;
    //more code
    ...
}
while(false);

但正如其他人所说,我不会这样做。

But as others have said, I wouldn't do it like this.

这篇关于do-while(false)的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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