0

what is different between while(0) and while (1 or another number ) ??

int main() {
   do {
      printf("Math ");
   } while (0);

   printf("Computer");
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
Amirpr
  • 9
  • 1
  • 3
    When you tried, what did you observe? – Yunnosch Dec 14 '19 at 16:03
  • Does this answer your question? [Why use apparently meaningless do-while and if-else statements in macros?](https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros) – gstukelj Dec 14 '19 at 16:25

2 Answers2

4

while (cond) { ... } and do { ... } while (cond); execute the loop as long as the condition is true. The former checks the condition before each pass of the loop, while the latter checks the condition after each pass of the loop.

0 is a false value.

1 and other numbers are true values.

while (0) { ... } is useless. It's a loop that will never be executed.

while (1) { ... } is a infinite loop. Something like break, return or exit is needed to leave it.

do { ... } while (0); is a weird way of writing { ... }.[1]

do { ... } while (1); is a weird way of writing while (1) { ... }.


  1. do { ... } while (0) has use in macros.
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 2
    `do {} while(0)` is not weird way and it is needed in the multi statement macros as in my answer – 0___________ Dec 14 '19 at 16:14
  • @P__J__, 1) I said `do { ... } while (0);` was weird. You did not use that in your macro. 2) Even if it's useful in that situation, that doesn't make it make it any less weird. /// But yeah, that use is good to know, though far too advanced for the OP. – ikegami Dec 14 '19 at 16:22
  • @ikegami It's actually common practice, see https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros for example. – gstukelj Dec 14 '19 at 16:26
  • @gst, 1) That doesn't use `do { ... } while (0);` 2) I know it's common to use `do { ... } while (0)` in macros. I'm not sure why you're telling me that. – ikegami Dec 14 '19 at 16:27
  • @ikegami . *I'm not sure why you're telling me that* - because from your answer we do not feel that you know that. BTW I do not use it too often as I bare myself if I only can from that kind of macros (prefer `inline` functions) – 0___________ Dec 14 '19 at 16:31
  • @P__J__, gst claimed (through the use of "actually") that I said it wasn't common practice (to use it in macros). Nothing in my answer or subsequent comments said that. So again, I have no idea why they claimed that. – ikegami Dec 14 '19 at 16:33
  • @ikegami the word `weird` suggests that (at least I feel it this way) – 0___________ Dec 14 '19 at 16:37
  • 1
    @P__J__, 1) For the third time, I said `do { ... } while (0);` is weird, but the macros don't use `do { ... } while (0);`. 2) `do { ... } while (0)` is a hack to make multi-statement macros appear as a single statement. That is weird usage of a loop, even if common. 3) I've also already stated that I think it's inappropriate to confuse the OP with this. But I've added a footnote – ikegami Dec 14 '19 at 16:46
1

in C all non zero values are the "true" and zero is "false". So if the number is used as the logical expression it acts as the logical value of true or false. You can even use pointers as a logical values

char *ptr;
if(*ptr)
{
   //do something if ptr is not NULL 
}
else
{
   //do something if ptr is NULL 
}

The do {... } while(0) is used in the multi statement macros. Why?

Lets analyze the if statement:

if(condition)
    macro(x);
  else
   foo();

if the macro is just the simple compound statement like:

#define macro(x) {x *= 2; printf("%d\n",x);} then the if statement will not compile

but if we define the macro this way:

#define macro(x) do{x *= 2; printf("%d\n",x);}while(0)

everything is fine and syntactically OK.

You can test it yourself here: https://godbolt.org/z/RW7Zmo

do {...} while(0) will execute only once as the condition is checked after the statements inside the {}

0___________
  • 60,014
  • 4
  • 34
  • 74