How this swapping works? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 5

How this swapping works?

b=a-b+(a=b)

14th Jul 2019, 12:06 PM
[ ]
[     ] - avatar
4 Réponses
+ 2
Hatsy Rei ChillPill Gordon KrOW ....pls help me understand how this expression is evaluated
14th Jul 2019, 12:09 PM
[ ]
[     ] - avatar
+ 2
ChillPill thanks
14th Jul 2019, 1:59 PM
[ ]
[     ] - avatar
+ 2
Operator precedence is the key about this intelligent swap expression. The equal to '=' operator accumulates Right hand side expression, from right to left, parenthesis is evaluated first which overwrite a's memory location with vale inside variable b, 11 with moving left there is + and - arithmetic operators which has equal precedence yields 5, stores it in left hand side b=5. Nice work ChillPill
15th Jul 2019, 12:48 AM
Prantik Sarkar
Prantik Sarkar - avatar
+ 2
This will assign the value of a to b. Since a=b ,the +b and -b will gets cancelled and results in b=a
7th Aug 2019, 1:56 PM
Nithish Nithish
Nithish Nithish - avatar