Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
So suppose you have a variable A. that stores a value of town and also you have a variable B, that
stores a value of twenty.
Nothing complicated so far.
Right.
And now your task is to swap between the values of these two variables.
And you may think to use some assignment statements that on one hand they may seem to be pretty much
okay.
Something like that, A equals to B and then B equals two A..
But you can see what happens behind the scenes.
So let's take a look at this whole process once again.
Let's just undo these last two operations.
And there you go.
Now we can see once again, assuming the first line where we say a equals to be a gets the value of
B.
This means that A has a value of twenty as well as B also has its own value of twenty.
So at this point, both of the variables hold the value of twenty.
And the next line of code is actually useless because A already does not hold the value of ten.
The value of ten is at this point, it's it's lost.
So if we tried the second line of code where B equals 2A, we just get the same 20.
So in this way we can say that the swap operation historically failed.
And to solve this problem, we may use a similar approach to the one we used in our previous example.
So for us to swap between these two variables, we will use additional variable that will help us with
our task.
We will call this variable, as we called it previously.
We will call it temp.
And it will be of the same type as the variables A and B..
And in this case, it's integer.
And this temp variable will actually help us here, just like it helped us in the previous example with
the shoes, the swapping general.
So before we do any swap operation, let's first of all print the values of variable A and variable
beat the screen.
This way, once we are done using our swap operation, we can see on our little console application
the results, the the actual values of variable A and B before the swap.
And after the swap, just to make sure that everything was was OK.
And now all that remains is simply to use the similar approach that we've seen in the swap general explanation.
And basically to say that temp will get the value of a.
So temp equals to a.
And there you go.
You can see that ten is now stored also in variable temp.
And then you just write A equals to B and then the variable A. gets the value of B and then you write
B equals to temp in the town.
The value of ten goes from variable, from box temp to variable B.
And now we can say that we are finished with our swap operation.
We can see that variable A contains the value of twenty and variable B contains the value of ten.
Which is exactly as expected.
And what we also would like to do is simply to print the values to the screen to make sure that our
work has succeeded.
So we will just use print F A equals two percentage D and bring the value of variable A..
After this WAP operation.
And we will do also the same for variable B.
So just print F, B equals two the G.
And just print the B value to the screen.
So this way you can make sure that all the operation that you can see the values before this was printed
on the screen.
And also after the swap printed to the screen.
So there shouldn't be any mistakes here.
Right, guys.
But there also may be times where the interviewer will tell you to implement these swap operation things
without using a third variable, without using these stem box.
And that's actually also possible in many ways.
As a matter of fact, bar to live it for another video.
So let me know if you would like me to create a dedicated video just for this topic, how to perform
a swap operation between two variables without using, using, without using guess or a third variable.
I'm tired after this swap thing.
So, as always, thank you guys for watching.
And I'll see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.