I just tried a very interesting number in the NOT operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I just tried a very interesting number in the NOT operator

If the age=16, the cout is still"your age is less than 16", which does not make sense, so when we use NOT ooerator, we must need to be careful about these sorts of special condition^_^

21st Aug 2018, 11:44 AM
Kevin Chu
4 Answers
+ 3
Your question doesn't make much sense without the actual full code.
21st Aug 2018, 11:50 AM
J.G.
J.G. - avatar
+ 3
I don't think that's a problem with the not operator. It's just that the if stament doesn't really make sense. If you know that a man is NOT older than 50 (>50), you still can't say that he's definitely younger than 50 (<50), because he might be exactly 50 (=50) years old as well. That's why < 16 is not the opposite/negation of > 16. The negation is <= 16 (less than or equal to 16). If you use the statement correctly, there's no problem: if(!(age>=16)) cout << "your age is less than 16" << endl; - or - if(!(age>16)) cout << "your age is less than or equal to 16" << endl;
21st Aug 2018, 4:30 PM
Anna
Anna - avatar
+ 1
This is one of the reasons why I try to avoid the not operator when possible, it can get confusing. I guess it's just human nature to have difficulty reading it. You can read it as if age is not bigger than 16 print ... that. But 16 is not bigger than 16 so it returns true. The output is a mistake in the lesson, the comments in that lesson also mention it but nothing has been done about it sadly. The output should be "Your age is less than or equal to 16". or the expression should be !(age>=16)
21st Aug 2018, 12:50 PM
Dennis
Dennis - avatar
0
Sorry, my bad.(^_^;) The code is used in the teaching of C++,the NOT operator: #include <iostream> using namespace std; int main(){ int age=10 if(!(age>16)){ cout<<"your age is less than 16"<<endl; } }
21st Aug 2018, 12:40 PM
Kevin Chu