Can someone explain why this while loop exits immediately even though the condition is true?
bool cond = false;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while ( cond == false ) ;
std::cout << " while loop completed " << std::endl;
return a.exec();
}
If we rewrite the loop to something like this, then it works as expected , i.e. it loops as long cond is set to false.
while( true ){
if( cond )
break;
}
cond
is a global variable and if we were to change it to a local variable, then the first (and second) while would loop infinitely.
This was built with a MingGW64
compiler in release mode.
1