Share via


Compiler Warning (level 4) C4127

conditional expression is constant

The controlling expression of an if statement or while loop evaluates to a constant. If the controlling expression of a while loop is a constant because the loop will exit in the middle, consider replacing the while loop with a for loop. You can omit the initialization, termination test and loop increment of a for loop, which causes the loop to be infinite (like while(1)) and you can exit the loop from the body of the for statement.

The following sample generates C4127:

// C4127.cpp
// compile with: /W4
#include <stdio.h>
int main() {
   if (1 == 1) {}   // C4127
   while (1) { break; }   // C4127

   // OK
   for ( ; ; ) {
      printf("test\n");
      break;
   }
}