ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How I can swap between value variables using two variables only?

user-image
تم إضافة السؤال من قبل ehab ab'oushi , IT Manager , INGOT Brokers
تاريخ النشر: 2013/06/25
Daanish Rumani
من قبل Daanish Rumani , Product Manager , Publicis Sapient

Let a and b be the two variables.
// Swap a and b a = a + b; b = a - b; a = a - b; Example Assume a =12, b =7 a = a + b; // a =19, b =7 b = a - b; // a =19, b =12 a = a - b; // a =7, b =12 But you should use a third variable when swapping.
The above method fails when a and b are float or double.
Also it cannot swap values whose addition is more that the max supported value.
i.e.
a + b > Int.Max for int type.