Public Group

This topic is 4032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the difference (in detail) between = and ==? and what is the difference between this code if(x = 3) and if(x == 3)? no clue why this is confusing me so much, my book tells me but it is very bleak and says it in such a way that confuses the heck out of me. This is not for homework or anything I'm teaching myself through books/internet referance. ty :D!

##### Share on other sites
x = 8 // this sets the varialbe x to a value of 8

x == 8 // this checks if x is equal to 8, and if so, then it is true

example use

x = 8;
if( x == 8 )
// do something since x equals 8
else
// do something since x does not equal 8

##### Share on other sites
Might want to not that in c++ it is legal to say
 if( x = 5 ) {}

What this does is sets x to 5, and if it worked (and there's no reason it won't) it executed whatever is in the if. That is why there is a difference between equals, and comparisons.

It's generally not a good idea to change the value of a variable in an if(), but you can and that's why we differentiate!

##### Share on other sites
Assuming C or C++.

Both of your examples are statements. A statement has a type and value.

Consider this:
"Hello World";
This is a valid statement with string type (const char *).
3;
is another valid statement that will be typed as integer (int).

x=3;
is an assignment statement. After it's executed, x will equal 3. The statement is typed the same way as x. If x is int, it will be typed as integer.

x==3;
is comparison. It'll compare two values for equality. It's typed as boolean (can be true or false).

In C/C++, a boolean is equivalent to an integer.

In an if block, this means the following:
if (condition) {}
condition here is checked whether it's true or false. False has integer value of 0, any other value represents true.

if (x = 3) {}
evaluates to
if (3) { // x has value of 3}
(and within the block, x now has value of 3.

if (x == 3) {}
evaluates either to
if (0) {}
or
if (1) {}

Side effect of this allows for some interesting code:
const char *s = "Hello World";int i = 0;while (char ch = s[i++]) {  std::cout << ch;}

s in this example is null-terminated string. Last character after d is 0.
While condition continues as long as the condition evaluates to true.
Given the previous rules, the following happens:
s[i++] // has value of character at given index, or 0 if we've reached end of stringch = s[i++] // assigns, the value of the expression is s[i++]char ch = s[i++] // stores the result of above statement in new variablewhile (char ch = s[i++]) // the while () will therefore continue until s[i++] is not 0

##### Share on other sites
Quote:
 Original post by Splinter of ChaosWhat this does is sets x to 5, and if it worked (and there's no reason it won't) it executed whatever is in the if.

it always works. The statements will be executed only if x is non-zero.

##### Share on other sites
I won't ALWAYS work, I guess.

If Antheus is right and
if( x = 3 ) is the same as if(3) { \\x=3 }

then
if( x=0 ) would be the same as if(0) {} else {//x=0}

I can't test any of this right now, though. I'm in the process of switching IDEs.

##### Share on other sites
ya, if you do something like

if(x = 0){  ...code...}

the code in between { } will never execute

##### Share on other sites
Quote:
 Original post by AntheusA statement has a type and value.

I don't think so. A statement is an expression with a semicolon appended to it. Once you have a statement, the type and value of the expression are lost. Your reasoning applies to the expressions, however.

x = 3

is of type int and has the value 3, and

x == 3

is of type int and has a value that represents true (i believe it's 1).

Note the "missing" semicolons.

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×