bool != bool

What is difference between the those two codesnippets (first one doesn't work, second does)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool status = true;

//---------------
//doesn't work:
if (CONDITION)
{
    status != status;
}

//---------------
//does work:
if (CONDITION)
{
    if (status)
    {
        status = false;
    }
    else
    {
        status = true;
    }
}


Thanks for an explanation (especially about why the first won't work).
The first one does not work as expected because != is NOT an assignment operator like +=.

!= is the NOT EQUAL test (and returns true or false)

To swap/toggle the state of the status, you can use

status = !status
Last edited on
Ah, thanks :)
may be you want mean this

status = !status ; // there is an space between = and ! , is quite different from next line
status =! status ;
Last edited on
=! is not a C++ operator so writing
status = !status and status =! status
is the same thing
...only slightly less clear. I like to use whitespace for clarity, even if it is something small like that. No big deal really, though; both compile and do the same thing.
Topic archived. No new replies allowed.