while 语句中的 0

发布于 2024-12-10 14:45:03 字数 299 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

演出会有结束 2024-12-17 14:45:03

它只做某件事一次。它广泛使用在宏中对语句进行分组并使用法更自然(即需要结束分号)。

任何有价值的编译器都应该完全忽略 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.

往事随风而去 2024-12-17 14:45:03

虽然这个“循环”只执行一次,但可以这样使用它:

do
{
  // declare some local variables only visible here
  // do something

  if(TestSomeConditions())
      break;

  // do something more
  // destructors of local variables run here automatically
}while(0);

恕我直言,在大多数情况下,最好以更结构化的方式重写它,或者通过这种方式

  // do something
  if(!TestSomeConditions())
  {
      // do something more
  }

或通过引入一个单独的函数来反映原始范围:

  void MyFunction()
  {
      // declare some local variables only visible here
      // do something

      if(TestSomeConditions())
          return;

      // do something more
  }

Though this "loop" is executed just once, one may use it this way:

do
{
  // declare some local variables only visible here
  // do something

  if(TestSomeConditions())
      break;

  // do something more
  // destructors of local variables run here automatically
}while(0);

IMHO, in most cases it is better to rewrite this in a more structured manner, either this way

  // do something
  if(!TestSomeConditions())
  {
      // do something more
  }

or by introducing a separate function, reflecting the original scope:

  void MyFunction()
  {
      // declare some local variables only visible here
      // do something

      if(TestSomeConditions())
          return;

      // do something more
  }
尘曦 2024-12-17 14:45:03

它将把 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).

抠脚大汉 2024-12-17 14:45:03

do while 循环首先执行循环体中的所有指令,然后检查条件。因此,该循环仅执行循环体的指令一次。

do
{
    //my instructions
}while(0)

相当于写

{
    //my instructions
}

现代编译器足够聪明,可以优化这些东西!

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.

do
{
    //my instructions
}while(0)

is equivalent to writing

{
    //my instructions
}

The modern compilers are smart enough to optimize these things out!

醉态萌生 2024-12-17 14:45:03

从技术上讲,它使其运行一次,因为它在运行一次后进行检查(并检查失败)。

除此之外,它还用在宏中。例如:

#define blah(a) \
        something(1); \
        something(2)

if(a)
    blah(4);
else
    blah(19);

会导致这样的情况:

...
if(a)
    something(1); something(2);
else
    something(1); something(2);

这不是有效的语法,因为 else 不再是 if 语句的一部分。您可以做的是:

 define blah(a) \
    do {
       something(1); something(2);
    } while(0)

    ...

这将转换为 if 块中的单个语句:

if(a)
    do { something(1); something(2); } while(0);
else
    ...

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:

#define blah(a) \
        something(1); \
        something(2)

if(a)
    blah(4);
else
    blah(19);

would lead to this:

...
if(a)
    something(1); something(2);
else
    something(1); something(2);

which is not valid syntax, as the else is not part of the if statement anymore. What you can do is:

 define blah(a) \
    do {
       something(1); something(2);
    } while(0)

    ...

which would translate into a single statement in the if block:

if(a)
    do { something(1); something(2); } while(0);
else
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文