what's the difference

Hello!
in a case where int a=5;
if((!a)++) or (++(!a)) gives me an error message saying that the operator ++ requires l-value, but the boolian result of if (!a++) is false.
If someone would be so kind to explain this!
could you post the whole code please
Hey zprise, it's tricky without the whole code but have you tried
if (a != 5 (a ++))

I can't really say it will work, next time give us the whole code or atleast
the problem section of the code.
Whoops, my first version of this post was ENTIRELY WRONG -- very embarrasing -- and this is a complete rewrite;

I had to go back to the books to clarify how the negation operator (“!”) works and the following is a better discussion of the topic;

First, you can apply the negation operator "!" to ANY "arithmetic type" - which includes boolean, integer and float values.

BUT it only returns a boolean value of “1” or “0”

So, thinking about “!”how it treats each arithmetic type;

Boolean: either "1" or "0" and "!" simply reverses them.

For a non-zero integer or float, "!int" (or "!float") evaluates to "0" otherwise "!0" evaluates to "1" - so...

!a is valid when a is of boolean, integer or float type.

-- If a = 0 for boolean, int or float then !a = boolean value of 1

-- If a = 1 for boolean then !a = boolean value of 0

-- If a > 0 for int or float then !a = boolean value of 0

The following code demonstrates the above;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    float test = 0;
    for (int count = 0; count < 10; count++) 
    {
        cout << "\ncount = " << count << "  !count = " << !count;
        test = count;
        cout << "\t\ttest = " << test << "  !test = " << !test;        
    }    
    cout << "\n\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


SO - to answer your original question...

if int i=5, then;

(!a++) is "false" because;

a++ gives an integer value of 6, and !6 gives a boolean value of 0

why ((!a)++) or (++(!a)) gives an error is trickier, but the responses my compiler gave to the following suggest something consistent is happening;

1
2
3
4
5
6
  bool a = "0";
  cout << a;        // this will work
  cout << !a;       // this will work
  cout << a++;      // this will work
  cout << !a++;     // even this will work
  cout << (!a)++;   // but this will give the error "non l-value in increment" 


I don't know what is happening here, but it seems an intriguing question. I'll see if I can find an answer...
Last edited on
Thank you Mr muzhogg
Your info was quite helpful
when the code was
1
2
int a=5;
if ((!a)) cout<< a;

The computer would obviously not print a.
and if the code was
1
2
int a=0;
if ((!a)) cout<< a;

The computer would print a.
but when the code was
1
2
int a=5;
if((!a)++) cout<< a;

The compiler would give an error message.
and yet when
1
2
3
int a=5;
if(!a++) cout<<a;
the ouput would be 6.
Sorry about the earlier wrong information :) but with a clearer understanding of "!" I think the above seems quite easy to figure out;

for;
1
2
int a=5;
if ((!a)) cout<< a;


(!a) would evaluate to "0" giving if(0) and so NOT printing a

for

1
2
int a=0;
if ((!a)) cout<< a;


(!a) would evaluate to "1" giving if(1) and so printing a ("0")

for

1
2
int a=5;
if((!a)++) cout<< a;


(!a) would evaluate to a boolean value "0" which also happens to be a temporary constant -- so the system will now try to apply ++ to a temporary constant, which is not allowed, so returns an error (does it say something about "non l-value in increment" or some-such?

For
1
2
int a=5;
if(!a++) cout<<a;


This throws me a bit. I would have thought that the system would first increment a (as ++ takes precedence over !) to give a = 6, and THEN apply "!" giving if(0). What I can't work out is why the system THEN prints a? I tried it on my system and it works just as I have described - that is, it does NOT print a after evaluating (!a++).

I notice that your original question was why does (!a++) evaluate to false for int a=5 - so surely my thinking here is correct.

Anyway, sorry for the mistake I made in my previous post (now corrected)

Last edited on
Worked it out!!!

When the system evaluates (!a) it returns a CONSTANT value which is either "0" or "1".

And because you can't increase or decrease a constant -- which means not being able to use either the ++ or -- operators -- then the system rejects all of the following;

(!a)++
++(!a)
(!a)--
--(!a)

By omitting the parenthesis, the system is doing something different - working with the variable value a rather than a constant value (!a) (where the ENTIRE EXPRESSION "(!a)" is the constant)
Last edited on
To Mr. muzhogg who spent so much time to help end to all viewers,
If I now understand ,then with ((!a)++) ,first !a is evaluated as the boolian result false, but then a boolian result can not be incremented.
But with (!a++), first a is incremented, and only then is ! evaluated. i.e. (!a++) ie equel to (!(a++))
What do you say
I think you're about right, but it's probably worth making a distinction between a boolean variable and a boolean constant and whether either can be incremented.

If you try;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    bool b1 = false;

    for (int i = 0; i < 10; i++)
    {
        cout << "\ni = " << i << "  b1 = " << b1;
        b1++;

    }
    
    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


you might be surprised to find that you CAN increment the boolean VARIABLE b1.

In fact, for a boolean VARIABLE b1;

if b1 = 1 (true) then b++ = true while b-- = false;
if b1 = 0 (false) then b++ = true while b-- = false;

BUT if you change the above code so that b1 is a boolean CONSTANT;

1
2
3

    const bool b1 = false;      // instead of bool b1 = false;


then b1++ and b1-- will both give you compile time errors.

What this all means is that the reason ((!a)++) doesn't work is NOT because a boolean value cannot be incremented (it can) but because the value represented by (!a) is a constant and a CONSTANT cannot be incremented.

And just to press things a little more: the fact that (!a) gives a BOOLEAN constant really isn't the issue. For instance, the following won't work either;

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
    
    int x = 2, y = 6, z;
    
    z = (x * y)++;           // not allowed       
    
   return 0;
}


because the compiler stores the result of (x * y) as a constant, and therefore cannot increment it.

In fact, if you try compiling the above piece of code you'll an almost identical error as you mentioned in your original post -- the one about the l-value.

So, to sum up, you're right to say (!a) cannot be incremented but NOT because it is a BOOLEAN value, but because it is a temporary CONSTANT.

On the other hand, when faced with !a++ the system is working with the variable a which you defined in your code. This means it is able to apply the ++ operator. As for the order, if you consult a list giving the precedence of C++ operators you'll see that ! and ++ have the same precedence. But they are what is known as associated right-to-left - which means the system reads them from right to left (a list of C++ operators should give you this information too). So, ++ gets applied first, followed by !. If you wanted to do this the other way around, you would need to have two operations;

1
2
3
4
5

    // to calculate the equivalent of (!a)++
    a = !a;
    a = a++;


Regards,
muzhogg.
Last edited on
Can anybody help me to findout the result of the following codes with explanation
1. b=a+++++a if a is 10 initially.
2. b=++a+a++ if a is 10 initially.
Does both codes have the same result?
Last edited on
If a = 10 in both cases they both end up 22.

its a little misleading the way it is written right now
1. ) b = a++ + ++a;
2.) b = ++a + a++;

The order of operation for this is line is ++a, +, =, a++
first do pre increment (++a) now a = 11
1.) b = a++ + a
2.) b = a + a++

not add a's and assign b
b = 11 + 11
b = 11 + 11

now post increment (a++)
at the end both a's end up being a=12

This code should be simplified so its more obvious to what it does such as
int a = 11;
int b = 2 * a;
a++;
Thankyou Mr.dolorx, Thankyou very much for your explanation regarding the increment operator. I was a little bit confused about the precedence of the prefix and postfix operators. Now it's OK. Thank you once more.
Topic archived. No new replies allowed.