Symbols

closed account (LN7oGNh0)
I see these 'symbol' things (dont know what they are actually called...) and I get confused for what they are supposed to do.

++

!

--

I dont know when you would use them and an example of when they are used would help a lot. Lastly, I was just wondering, I was typing this code, and when I used an equal sign it didnt work. But then I put a double equal sign and then it worked. I dont have time to post the code, but can anyone tell me why in some cases I would need to type this in?

Thank you.
The first and last are the increment and decrement operators respectively. The ++ increments a value by one. For example, int x = 0; x++;. The value of x after these 2 statements is now 1. The decrement does the opposite, or decreases the value by 1.

The second is a logical operator, which also include the and (&&) and the or (||) operators. It is used in test expressions and is termed the logical NOT operator. As such: if (! x == 1) This bit of code basically says "If x is NOT equal to 1" (note the difference between = and ==. = is an assignment operator, as in x = 1 assigns the value of 1 to x. == says is equal to, and compares the value of x to 1 without making x equal 1.) Test expressions return a value of 1 if true, or 0 if false. So if x did equal 1 in this expression the value of the expression would be 0, since the not operator takes the opposite of whatever expression it is included with. Note also that only 0 is false. Any other value is considered true, even negative values.

Here's a good page for explaining all the operators: http://www.cplusplus.com/doc/tutorial/operators/

Here's a little more on boolean operations to understand the different between and, or and not: http://www.cplusplus.com/doc/boolean/
Last edited on
I was typing this code, and when I used an equal sign it didnt work. But then I put a double equal sign and then it worked.

The important thing is that = on its own will change the value of something. The double equals == simply tests the value, but leaves it unaltered.

For example,
1
2
3
4
5
6
7
8
9
10
    int x = 5;
    int y = 17;

    // test to see whether x is equal to y.
    cout << (x == y) << endl;
    cout << "x: " << x << "  y: " << y << endl;

    // change x so that its value is now the same as y.
    cout << (x = y) << endl;
    cout << "x: " << x << "  y: " << y << endl;
Output:
0
x: 5  y: 17
17
x: 17  y: 17


Explanation of output
0 means false, x is not equal to y.
original x and y are displayed.
17 means true. It is also the value of the expression, that is the value of x.
New values of x and y are displayed.

When it comes to conditional expressions, such as if (something) or while (something), we normally think in terms of true and false. But the compiler will also treat a zero value as meaning false, and any non-zero value (such as 17 in the example) as meaning true.

Also, you can do this: cout << (5 == 3) << endl;
But this is an error: cout << (5 = 3) << endl;
The first is comparing 5 and 3.
The second tries to change the value of 5 to make it 3. That doesn't make any sense, either to the compiler, or to us.



Last edited on
Topic archived. No new replies allowed.