Sign in to follow this  

detailed explenation please :(

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 string
ch = s[i++] // assigns, the value of the expression is s[i++]
char ch = s[i++] // stores the result of above statement in new variable
while (char ch = s[i++]) // the while () will therefore continue until s[i++] is not 0

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
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.


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
A 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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this