NOT statement C++

This is going to be a long post, so bear with me…

I decided to learn C++ and I’m doing great, until I got to the NOT statement.

First, let me say, I understand && and || statements perfectly. I wrote my own programs, compiled, tested and they worked great.
Second, I understand the NOT statement but I don’t know how to apply it to a test program. I understand that if the code evaluated returns false, it satisfies the NOT statement making it TRUE. ie. ! (6 <= 4) - it would return TRUE because 6 IS NOT less than or equal to 4.

So with that said, I wrote this expecting to not satisfy the NOT statement making it true, but the output was not what I wanted to.
Also, when I compile, Terminal did not find any BUGS, so it’s not my programming
Here is is:

// NOT Statement - demonstrate the NOT statement

#include
#include
#include

using namespace std;

int main(int nNumberofArgs, char * pszArgs[])
{
// declare a variable to store an entered number to be tested with the NOT statement
int nNum;
cout << "Enter a number that is not equal to or less than 4: ";
cin >> nNum;

      // by declaring a number that is not equal to or less than 4 we will satisfy the NOT statement


      // now create the IF statement to test the NOT statement
      if ( ! nNum <= 4)
      {
                cout << "The number you entered was not less than or equal to 4" << endl;
      }
      else
      {
                cout << "The number you entered was less than or equal to 4" << endl;
      }

      // Ergo if we enter say '6' we will get the first output because the argument on the right is true making the expression false
      // If I would have put say '3' that would make the expression false because the argument on the right is true: 3 is less than or equal to 4

      // wait until user is ready before terminating program
      // to allow the user to see the program results
      system("PAUSE");
      return 0;

}

So I compiled and ran the program. I entered ‘6’ and I got the first output - like expected.
I then re-ran the program this time entered a number that would make the expression false because the argument on the right would be true, entered 3, but I got the first output “The number you entered was not less than or equal to 4”, but 3 is less than 4, so my program failed to execute what I wanted it to.

Can anyone help me? To me this is the error, but i don’t know what the right code is:

if ( ! nNum <= 4)

I believe that my syntax or the NOT expression is incorrect…

You need an extra set of parenthesis in there.

if ( ! nNum <= 4)

This code is evaluating !nNum before it even gets to the less than comparison.

So, you need to isolate that test first…

if ( ! (nNum <= 4))

This will force the nNum <= 4 to be evaluated first.

Obviously there are better ways to do this particular test but if you’re just learning about operators and precedence it’s fine.

Thank you! I understand that now! I tested it, and it worked!! :slight_smile:

Cheers

Just for fun, I’m going to see if I understand why you saw that error:

if ( ! nNum <= 4)

Booleans in C++ are stored as integers. 0 is false, anything else is considered true. If nNum is 3 (“true”) then !nNum would be false, or 0, and 0 is less than or equal to 4.

This is why some people talk about the “dangers” of C++. In a language like Java you’d get an error message saying something like “Hey! nNum isn’t a boolean!”

Parenthesises will help in fact. What happens: Something called “operator precedence” says “!” is stronger than comparison operators like “<=”.

Using parenthesis to explain this precedence means:

! nNum <= 4

is evaluated as:

(! nNum) <= 4

!nNum for type “int” and a value of 3 (0x0003) for nNum evaluates to -3 (0xFFFC), because the “!” operator, applied to an integer is seen as a bitwise inversion. To check this, you can enter -6 and you will get the second output.

@drcouzelis: Yes, the ability to redefine per type what an operator does is helpful but also the cause of many errors.

[quote=fishpond]
!nNum for type “int” and a value of 3 (0x0003) for nNum evaluates to -3 (0xFFFC), because the “!” operator, applied to an integer is seen as a bitwise inversion. To check this, you can enter -6 and you will get the second output.[/quote]

No. The operator ! is a unary logical negation. If nNum is any number other than zero, the value !nNum is false (zero), and if nNum is zero the value of !nNum is true (1). So your suggested verification, of course, does not work.

In fact a good C++ compiler (such as the one provided by GCC) with optimisation enabled will elide the condition itself and the second branch of the if condition entirely, removing the alternative message from the resulting binary because domain and range evaluation proves this can never be output and so there’s no need to emit code that can do so.

I’m looking forward to solve your problem, thank you for sharing it… get soundcloud re-posts