This code doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int a = 0;

int main()
{
    if ((a) == (a = 1))
        {
            if (a)
                {
                    cout << "Failure.\n\na = " << a << ".\n" << endl;
                }
            else cout << "Complete failure.\n\na = " << a << ".\n" << endl;
        }
    else
        {
            if (!a)
                {
                    cout << "Partial failure.\n\na = " << a << ".\n" << endl;
                }
            else
                {
                    cout << "Success.\n\na = " << a << ".\n" << endl;
                }
        }
    return 0;
}


What I'm trying to do one line 8 is, take the value that is stored in A, hold on to it, put a new value in A, and then compare the new value of A with the old value. I guess what I'm asking is are boolean expressions passed, or evaluated by value or by reference?

i.e. when the compiler reads a =, does it compare the value that is stored in a at that moment, or does it or does it use the address?

What it appears is it passes the address, so that assignment within the if expression like I have tried will fail to compare the initial value, which is what I want.

I'd like to be able to use the comma operator, so something like this:
 
if ((a) == (a = 1), a)

but it dawned on me that here the comma operator is ignoring everything up to the final a, which is not what I want. I want the comma operator to ignore just the asssignment so that what I get is something akin to:

 
if (A1 == A2)

where A1 is the initial value of a and A2 is the final value of a, but without declaring new variables.

Thanks for your help.
if ((a) == (a = 1))

what you are saying to the compiler :

"If a is equal to set a equal to one"

There is no comparison going on.
This code also does not to what it should do:
 
if (a = 1 == (a))
And this one is just really so weird that I just had to post it.
 
if ( ( (a) = ((1) ) == (a) ) )

Do I really have to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int a = 0;
int value(int x){return x;};

int main()
{
     cout << "First a = " << (a = 0) << ".\n" << endl;
      if (value(a) == (a = 1))
          {
              if (a)
                  {
                      cout << "Failure.\n\na = " << a << ".\n" << endl;
                  }
              else cout << "Complete failure.\n\na = " << a << ".\n" << endl;
          }
      else
          {
              if (!a)
                  {
                      cout << "Partial failure.\n\na = " << a << ".\n" << endl;
                  }
              else
                  {
                      cout << "Success.\n\na = " << a << ".\n" << endl;
                  }
          }
      return 0;
}
What I'm trying to do one line 8 is, take the value that is stored in A, hold on to it, put a new value in A, and then compare the new value of A with the old value.


You really need to break this habit of yours. You always want to try and cram as much as possible onto one line.

Don't.

New lines don't cost anything. They just make the code easier to understand, follow, and they reduce the number of goofups and errors (like the problems you're having in this thread).

This is easily accomplished like so:

1
2
3
4
5
6
int olda = a;
a = 1;
if(olda == a)
{
  // ...
}


Also notice that with this approach I don't need to explain separately what I'm trying to do. It's obvious with the code I'm writing.


And no, the solution you gave with your 'value' function will not work, because the compiler is free to evaluate expressions in any order it chooses between sequence points. So it very well could be calling that function with the new a value instead of the old one.

The original post has the same problem. The compiler can evaluate those in any order.
Last edited on
Topic archived. No new replies allowed.