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^_^
4 Respuestas
+ 3
Your question doesn't make much sense without the actual full code.
+ 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;
+ 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)
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;
}
}