+ 1

How to swap 2 variables value?

How to swap value of 2 variables without add 3rd variable ??

14th Feb 2017, 1:36 PM
Moh Affan
Moh Affan - avatar
4 Respostas
+ 7
a += (b - (b = a)); works fine, and it's only 1 line of code!!😀
14th Feb 2017, 1:51 PM
Kamil
Kamil - avatar
+ 3
a = a + b; b = b - a; b = -b; a = a - b;
14th Feb 2017, 1:41 PM
Yan Markov
Yan Markov - avatar
+ 3
@Kamil, @Yan: Great simplification of code. Watch out when the variables don't have infinite precision, though –you could be lead to accuracy issues.
14th Feb 2017, 5:46 PM
Álvaro
0
@Kamil, great and simple, @Kamil @Yan Markov, thanks...
17th Feb 2017, 11:24 AM
Moh Affan
Moh Affan - avatar