+ 22
[Solved] Is Anwer is 7 or 8?
int x = 15, y = 7; if (++x <= 15 && y++ == 7) { Console.Write(x); } else { Console.Write(y); } It's a challenge question and answer is comig 8. Is it right? https://photos.app.goo.gl/wZJpyPmhqTZzHQhD7 Edited - Sorry I didn't notice. There is single & not &&
29 odpowiedzi
+ 24
8 is correct, because if you take a look at the image, you can see you are applying the binary AND operator, not the conditional AND operator.
If you were applying the conditional AND operator, 7 would be correct, because due to Short Circuit Evaluation, the increment of 'y' would never be executed, and it would remain 7.
However, with the binary AND operator, both parts are necessarily evaluated, so 'y' is actually incremented, however, the condition is nonetheless false because 0 & 1 => 0.
So the else-statement is executed and 8 is printed.
You can verify this by running both versions within the SL playground.
+ 8
In Your Question Output of Code : 7
AJ #Level 20 End Day In that Challenge there is a Bitwise And Operator & and
Not a Logical And Operator &&.
In Logical (&&) And Operator, if first condition is False ,then Second Condition is not Check.
In Bitwise (&) And Operator , weather the first condition is ture or False , second condition is also checked.
In that Challenge First ++x<=15 is false and now x=16,
because of Bitwise (&) And Operator then second condition y++==7 ,this condition is True now y= 8
In if Statement there is (0 & 1) ,so this is false.
and output is 8.
+ 8
AJ #Level 20 End Day , there is difference in your code and the challenge question - you write logical and "&&" instead of bitwise and "&". Bitwise and is the reason for incrementing y.
+ 5
Jay Matthews It's correct but you didn't notice image and I also. There is single & bitwise operator.
+ 3
Shadow Thanks I didn't notice.
+ 2
Vyacheslav Krasnov It's already solved and also your calculation is wrong. Check image also.
+ 2
Before I saw the code I was worried about a question like that. My intuition was right 🙃
+ 1
8 is right
+ 1
8 is the answer
+ 1
8
+ 1
8
+ 1
8
0
8
0
8 is the Answer
0
8 is the Answer
0
Answer is 8, because
±±x=16 before and after condition
y++=7 before condition, and 8 after condition
(16<=15) and (7=7) - false
Console.write(y) - 8, because later y++
0
Yes 8 is Correct Answer.
0
8
0
Very good
0
So because of the && operation variable in the expression the value of y is 8 due to the binary 0-1 output. Otherwise I still would say seven.