How to swap values of two variables with or without a third variable

preview_player
Показать описание
Want to learn programming and get a job?
In this video, I'm explaining one of the frequently asked interview questions, and also a question that students often get on their exams -"How to swap values of two variables?". I'm going to give you two solutions for this problem: using a third variable or without using a third variable.

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

Follow me on other platforms:
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

CodeBeauty
Автор

best person I've ever seen who explains c++, thank you too much...

mohammednasr
Автор

The second solution has a possible overflow problem - that is, a + b must not be larger than the largest possible int (the exact size depends on the compiler)
To get around this problem, we can use xor

For example, if
a: 1010
b: 0010

b = a xor b
a: 1010
b: 1000

a = a xor b
a: 0010
b: 1000

b = a xor b
a: 0010
b: 1010

which is what we want :)

jakobilse
Автор

I really love the way you explain things and the fact that you take from you own free time to make those videos in order to help people to learn or get better at programming without asking for anything in return ;-) A big thank you for that !!! :-)

toastyren
Автор

You can also use inbuild function ( swap(a, b) )

WhoAmI-jqxh
Автор

I hope this question will be asked in tomorrow's interview.

agnesakne
Автор

Svaka cast Seldina! Predobro objasnjavas.

kenan
Автор

Hello Mrs how are you?
The swap value solution temp is the best 😍

sr.shakilmiah
Автор

Thanks for posting this - I'd not seen the second method (swapping the contents of two variables without using a third variable) before.

vkig
Автор

This is a cool explanation, but I would simply use the "swap" function which can do that very easy for you without the need for the temp variable, but on the other hand this is a more creative solution so definitely a thumbs up!

salesman-zw
Автор

This is kind of a good metaphor to explain swap for me, a beginner learning C++. Thanks!!!!

shawnxu
Автор

I love the way you explain it. You did great job. Thank you so much!

saadalsabagh
Автор

That was a nice challenge using the original 2 variables. I also tried swap(a, b); which also worked.

wattstudio
Автор

I love the example. I remember through(images) I have seen . I was having a hard time remembering this variable was this but now it is changed to that. When you had the two gasses of liquid I could picture that in my mind easily. For some reason that example made it so clear. Great job you get and A++ !!

mod
Автор

A very interesting demo that had shown in this video, thanks saldina! appreciate the effort!

eng
Автор

Amazing you’re not just teaching us coding you’re also teaching us mathematical solutions and this will open to us doors into software engineering thank you. And at first when i was searching for a teaching videos I usually avoid women videos but I decided to give your videos a shot and you turned out to be better than any other 👏👏👏

saudalkhazriji
Автор

I just started to learn C++ from you. Hope soon will start write small programs.

jansherkhan
Автор

Thank you, your expression way is like that of "code beauty ".

melkamuterefa
Автор

For integers its better to use bitwise xor instad of + and -

elico
Автор

I just want to say your explanation and your channel are awesome.
My question is that are you planning to discover something in Java or c# in the future?
My kind regards,

Mr.AIFella
join shbcf.ru