+ 17

Why the answer is 1?

It's a challenge question. The answer should be 0 but why the answer is 1? Please check the attached Screenshot https://photos.app.goo.gl/twngE2xh9NQZ17qp8

4th Aug 2020, 6:31 AM
A͢J
A͢J - avatar
23 Réponses
+ 20
hmm, AJ #L1G3 it's tricky though. But here's what I think: (Infix : (A-B++)) => (Postfix : (AB-)++) _____________________________________ <?php $n = 10; $n = $n - $n++; //(($n$n-)++) // $n = 10 - 10 = 0, so in line 3, we have n=0 but remember there's this postfix n++ which makes it 1 (like so (0)++ which is 1) again from 0 in line 3 after subtraction and assignment gets executed echo $n; //If you use the ++ operator as postfix like: var++.The original value of var is returned first then, var is incremented by 1.;  ?>
4th Aug 2020, 6:35 AM
minirkk
minirkk - avatar
+ 13
$n++ post increment $n-$n++ original value and after applying post increment original value will be increase by one that's why it will effect on output output is 1
4th Aug 2020, 6:46 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 10
Thanks Rohit,i think i got your explanation
4th Aug 2020, 7:45 AM
Ndeanaefo Chidalu
Ndeanaefo Chidalu - avatar
+ 8
Edward Yes in js will be 0. In PHP I got so many weird things. In PHP $a = 5 echo $a * $a++ will be 30. 😂😂
4th Aug 2020, 12:31 PM
A͢J
A͢J - avatar
+ 8
You should avoid using incremented (decremented) variable more than once between sequence points in languages with C/C++ language incement operator behaviour (like PHP). In Java, expression like int a=1; int b=a++ + a++ + a++; is absolutely valid and predictable (a is incremented right after using its value, Java has no operator overloading). I don't know php standard (php increment operator behaviour is the same as in C++), but according to C++ standard such code causes undefined behaviour since vairable is garanteed to be incremented only at the sequence points (it may be incremented or right after increment operator or later, but before sequence point)!!! $n=10 $n=n-n++ echo $n in C++ such code also gives 1, because (minus) is an operator which is function with operands a and a++ : n=n-n++; is the same as n = operator-(a, a++) The order of evaluation of function operands is not defined, but function call is a sequence point, so operands must be evaluated before function call. On my compiler operands evaluated from right to left (but it is not guaranteed). Right operand a++ is evaluated first, it evaluates to 10 and increments variable a to 11, the left operand now 11. operator-(11, 10) gives 1. $a = 5 echo $a * $a++ will be 30 in C/C++ such code also gives 30, because operator*(a, a++) Right operand a++ is evaluated first, it evaluates to 5 and increments a to 6, the left operand now 6. operator*(6, 5) gives 30. But again, it is code with undefined behaviour. Don't use it!!! Undefined behaviour doesn't mean random result, it means that result of evaluation may be different on other platforms or versions.
4th Aug 2020, 1:43 PM
andriy kan
andriy kan - avatar
+ 4
Its the after effect ☺
4th Aug 2020, 6:37 AM
Ro7ot
Ro7ot - avatar
+ 4
AJ #L1G3 😂😂😂😂
4th Aug 2020, 12:41 PM
🙂🙂🙂
+ 3
I'm wondering too 🤔(but in js it's 0)
4th Aug 2020, 6:48 AM
🙂🙂🙂
+ 3
AJ #L1G3 I think js is the strange one where => not a number is a number😎
5th Aug 2020, 6:21 PM
Ro7ot
Ro7ot - avatar
+ 3
Ro7ot 😂😂😂😂. Hahaha
5th Aug 2020, 7:19 PM
🙂🙂🙂
+ 2
Read about php operators it will help you https://www.w3schools.com/php/php_operators.asp
4th Aug 2020, 7:46 AM
Master Genius [ INACTIVE ]
Master Genius [ INACTIVE ] - avatar
+ 2
Sanjay Kamath Type casting? What do you mean?
6th Aug 2020, 5:09 AM
A͢J
A͢J - avatar
+ 2
Rahul Thakur Don't advertise your code in QA.
6th Aug 2020, 5:50 AM
A͢J
A͢J - avatar
+ 2
AJ #L1G3 I think he means, "coercion" in js
6th Aug 2020, 7:30 AM
🙂🙂🙂
+ 2
The lesson here is that don't rely on increments(++) 😂😂😂
6th Aug 2020, 7:32 AM
🙂🙂🙂
+ 1
i think should be 0. Happening like this 11-10=1. But it includes undefined behavior.. so it may 0 or 1...
4th Aug 2020, 2:32 PM
Jayakrishna 🇮🇳
+ 1
It is very simple ✨the variable $n is a post increment variable and the last screen output reflects this...0+1🍺
6th Aug 2020, 3:26 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Sanjay Kamath How it is simple? Just try same logic in JavaScript then see what happens.
6th Aug 2020, 3:34 AM
A͢J
A͢J - avatar
+ 1
AJ #L1G3 I think you are missing type casting in js...🤔
6th Aug 2020, 5:06 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
AJ #L1G3 (int) rather than var...
6th Aug 2020, 11:19 AM
Sanjay Kamath
Sanjay Kamath - avatar