logic error

hey guys...i have problem with this coding here..


#include <iostream>
using namespace std;

int main()
{
int found=0,count=5;

{
if(!found||--count==0)
cout<<"danger"<<endl;
cout<<"count="<<count<<endl;
}
system ("PAUSE");
return 0;
}

the discussion goes like this...my friend said that the output should be:
danger
count=4...but

it went like this,
danger
count=5

any idea?
Simple: what is if(!found || --code) supposed to do?
Check if (!found) or (--code) is true.
(!found) is true, so there is no need to check for (--code), and so it will never get executed, unless (found) isn't 0.
It's c++'s default optimization.
If you reverse them into if(--code || !found) (--code) will be executed, and the result will be the one you expected.
Last edited on
ohh...now i get it...thanks!!..
Topic archived. No new replies allowed.