logical operator question

can someone explain to me why the output would be ans is 5??

1
2
else if (!y||!x)	ans =0.5;
//why it is true? 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include<iostream>
using namespace std;
int  main()
{
int x =2, y=0;
double ans;
if (x<y)		ans=0.1;
else if (x==y)	ans=0.2;
else if(!x)		ans =0.3;
else if (!y && !x) ans=0.4;
else if (!y||!x)	ans =0.5;
else if (!y)	ans =0.6;
else 			ans=0.7;
cout<<"ans is"<<ans<<endl;

return 0;
}
Last edited on
|| operator returns true if either of its operand is true
so your code:
else if (!y||!x) ans =0.5;

the !x returns FALSE // because x is not a 0
the !y returns TRUE // because y is a 0

but FALSE || TRUE gives you TRUE
Last edited on
i still dont understand from the
 
int x =2, y=0;


why would y not qual to 0?? x is equal to 2???

well, lets back to basic,
1
2
else if(!x)		ans =0.3;
//this is return FALSE because !TRUE = FALSE right?? 
See my edited post, i mistook x for y before. Am sorry !
Last edited on
Topic archived. No new replies allowed.