+ 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 &&

4th Apr 2020, 9:57 AM
A͢J
A͢J - avatar
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.
4th Apr 2020, 10:07 AM
Shadow
Shadow - avatar
+ 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.
4th Apr 2020, 10:02 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
+ 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.
4th Apr 2020, 10:05 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 5
Jay Matthews It's correct but you didn't notice image and I also. There is single & bitwise operator.
4th Apr 2020, 10:41 AM
A͢J
A͢J - avatar
+ 3
Shadow Thanks I didn't notice.
4th Apr 2020, 10:13 AM
A͢J
A͢J - avatar
+ 2
Vyacheslav Krasnov It's already solved and also your calculation is wrong. Check image also.
5th Apr 2020, 8:55 AM
A͢J
A͢J - avatar
+ 2
Before I saw the code I was worried about a question like that. My intuition was right 🙃
5th Apr 2020, 9:59 PM
Yousef Eabitan
Yousef Eabitan - avatar
+ 1
8 is right
4th Apr 2020, 9:06 PM
Asif Suhel
Asif Suhel - avatar
+ 1
8 is the answer
5th Apr 2020, 7:54 AM
Charupriya Palaniswamy Rajendran
Charupriya Palaniswamy Rajendran - avatar
+ 1
8
5th Apr 2020, 4:18 PM
Mohammad Hossein Shamohammadi
Mohammad Hossein Shamohammadi - avatar
+ 1
8
6th Apr 2020, 5:46 AM
Mohit Nayak
Mohit Nayak - avatar
+ 1
8
6th Apr 2020, 7:25 AM
Mandal Vasu
Mandal Vasu - avatar
0
8
4th Apr 2020, 12:34 PM
Rajesh Dayasi
Rajesh Dayasi - avatar
0
8 is the Answer
4th Apr 2020, 5:19 PM
Mani Periyasamy
0
8 is the Answer
4th Apr 2020, 9:39 PM
Belazerava Viktoria
Belazerava Viktoria - avatar
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++
5th Apr 2020, 8:24 AM
Vyacheslav Krasnov
0
Yes 8 is Correct Answer.
5th Apr 2020, 2:41 PM
Tusar
Tusar - avatar
0
8
5th Apr 2020, 5:16 PM
Varun Kumar. Vidavaluru
Varun Kumar. Vidavaluru - avatar
0
Very good
5th Apr 2020, 10:01 PM
الصرمي سلوم
الصرمي سلوم - avatar
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.
6th Apr 2020, 12:55 AM
Carlos Nazario
Carlos Nazario - avatar