Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "} while (0);" always equal to "break;} while (1);"?

I have compared gcc assembler output of

do { 

// some code 

} while (0);

with

do {
 
    // some code

    break; 
} while (1);

The output is equal, with or without optimization but..

It's always that way?

No experiment can prove theories, they can only show they are wrong

like image 791
Hernán Eche Avatar asked Nov 27 '22 03:11

Hernán Eche


1 Answers

There is a slight difference:

do {
  // code
  if ( condition )
    continue;
  // code
  break;
} while(1);

Will restart the loop when condition is true, whereas in the } while(0); version, the continue will be equivalent to break.

If no continue is present, then they should produce exactly the same code.

like image 179
Gianni Avatar answered Dec 06 '22 13:12

Gianni