# Thread: Having a hard time interpreting this code

1. ## Having a hard time interpreting this code

I am going over questions from tests from previous semester of what the test I am going to take might be like and one question I do not understand is this:

What is the output of the following code segment?
Code:
```#include <stdio.h>

int main(void);
{
int a=6, b=4;

if (b = 0)

printf("One\n");

else if (a = 2)

printf("Two\n");

else if (a*b/5 > 4)

printf("Three\n");

else

printf("Four\n");

}```
when I run it in the compiler, the answer is "Two." How do you interpret this because I do not understand how the answer is "Two?"

2. It prints that because the code is broken.
Code:
`else if (a = 2)`
That does not compare "a" to the number 2. It assigns "a" the value of 2 (notice how it is using "=" instead of "=="). So basically, that code is the same as:
Code:
`else if (2)`
which will always evaluate to true.

3. Originally Posted by bithub
It prints that because the code is broken.
Code:
`else if (a = 2)`
That does not compare "a" to the number 2. It assigns "a" the value of 2 (notice how it is using "=" instead of "=="). So basically, that code is the same as:
Code:
`else if (2)`
which will always evaluate to true.
what is throwing me off is the a=6 and b=4. Am I supposed to plug that in for the a and b in the if-else if statement or no? Because that is where I am confused if they give me something like this on a test and I have to look at this and figure it out.

I understand what you said about the "=" and "==." I am still not getting it because of the int a=6 and b=4.

4. AFAIK the assignment inside the paranthesis is done before a is evaluated. a is 6 until you assign 2 to it, after that, you test if it is 2, which it is, so "two" is printed. Actually, I think it would not matter what value a has, as long as it's not zero.

5. Originally Posted by Subsonics
AFAIK the assignment inside the paranthesis is done before a is evaluated. a is 6 until you assign 2 to it, after that, you test if it is 2, which it is, so "two" is printed.
Wrong. As bithub points out, there is no such test. Note that assignment as a condition will return the value of the expression.

a = 2; // assigns the value 2 to a. ALWAYS TRUE*
a == 2; // TRUE if the value of a is 2, FALSE otherwise.

*unless the right side is 0, because the actual value of the expression is the final value of the left hand side, so eg. (b = 0) == 0, and 0 is FALSE.

6. Originally Posted by MK27
Wrong. As bithub points out, there is no such test. Note that assignment as a condition will always succeed.

a = 2; // assigns the value 2 to a. ALWAYS TRUE.
a == 2; // TRUE if the value of a is 2, FALSE otherwise.
Ha ha, I just edited my post. It's always true as long as it's not zero. I realize that it's the same as: if(a).

7. Originally Posted by BLG
what is throwing me off is the a=6 and b=4. Am I supposed to plug that in for the a and b in the if-else if statement or no? Because that is where I am confused if they give me something like this on a test and I have to look at this and figure it out.

I understand what you said about the "=" and "==." I am still not getting it because of the int a=6 and b=4.
The a=6 and b=4 are just there to confuse you. They don't actually have any effect on the output, because the values of a and b are changed.

Originally Posted by Subsonics
after that, you test if it is 2, which it is, so "two" is printed.
Not quite. The test is if it is non-zero, not two.

8. Yes, I realized this and changed my post. lol

9. Originally Posted by bithub
The a=6 and b=4 are just there to confuse you. They don't actually have any effect on the output, because the values of a and b are changed.
But wouldn't "two" get printed even if 2 wasn't assigned to a? since what is tested is only if a is not zero.

10. Originally Posted by Subsonics
But wouldn't "two" get printed even if 2 wasn't assigned to a? since what is tested is only if a is not zero.
if(a) would be true, yes, cause it is non-zero. Unless a is 0.

I have an old keyboard with a giant

==

at the top in permanent marker because of this.

11. Originally Posted by MK27
if(a) would be true, yes, cause it is non-zero. Unless a is 0.

I have an old keyboard with a giant

==

at the top in permanent marker because of this.
Yes, but my point was that the reason that the initial value of a doesn't have an effect on the output is not that it is changed to 2.

12. Originally Posted by Subsonics
Yes, but my point was that the reason that the initial value of a doesn't have an effect on the output is not that it is changed to 2.
Your point is wrong. The reason the initial value of a doesn't have any effect is precisely due to the fact that it was changed to 2. If the conditional was just:
Code:
`if (a)`
Code:
`if (a=2)`
then the initial value of a is what would determine the output of the conditional.

13. Originally Posted by bithub
Your point is wrong. The reason the initial value of a doesn't have any effect is precisely due to the fact that it was changed to 2. If the conditional was just:
Code:
`if (a)`
`if (a=2)`