NOT statement C++

Forum thread started by el.tigre.20 on Tue, 2012-07-03 19:50

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 <cstdio>
#include <cstdlib>
#include <iostream>
 
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...

Comments

Re: NOT statement C++

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.

Re: NOT statement C++

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

Cheers

Re: NOT statement C++

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!"

Re: NOT statement C++

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