Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,210 --> 00:00:06,900
So suppose you have a variable A. that stores a value of town and also you have a variable B, that
2
00:00:06,900 --> 00:00:08,540
stores a value of twenty.
3
00:00:08,730 --> 00:00:10,590
Nothing complicated so far.
4
00:00:10,610 --> 00:00:11,040
Right.
5
00:00:11,200 --> 00:00:15,750
And now your task is to swap between the values of these two variables.
6
00:00:15,870 --> 00:00:22,770
And you may think to use some assignment statements that on one hand they may seem to be pretty much
7
00:00:22,770 --> 00:00:23,390
okay.
8
00:00:23,460 --> 00:00:28,620
Something like that, A equals to B and then B equals two A..
9
00:00:28,830 --> 00:00:31,500
But you can see what happens behind the scenes.
10
00:00:31,950 --> 00:00:34,720
So let's take a look at this whole process once again.
11
00:00:34,770 --> 00:00:37,950
Let's just undo these last two operations.
12
00:00:38,100 --> 00:00:38,790
And there you go.
13
00:00:38,820 --> 00:00:45,510
Now we can see once again, assuming the first line where we say a equals to be a gets the value of
14
00:00:45,510 --> 00:00:45,870
B.
15
00:00:46,290 --> 00:00:52,860
This means that A has a value of twenty as well as B also has its own value of twenty.
16
00:00:53,040 --> 00:00:56,550
So at this point, both of the variables hold the value of twenty.
17
00:00:57,120 --> 00:01:03,390
And the next line of code is actually useless because A already does not hold the value of ten.
18
00:01:03,690 --> 00:01:06,930
The value of ten is at this point, it's it's lost.
19
00:01:07,040 --> 00:01:12,700
So if we tried the second line of code where B equals 2A, we just get the same 20.
20
00:01:12,870 --> 00:01:17,670
So in this way we can say that the swap operation historically failed.
21
00:01:17,790 --> 00:01:23,400
And to solve this problem, we may use a similar approach to the one we used in our previous example.
22
00:01:23,520 --> 00:01:30,270
So for us to swap between these two variables, we will use additional variable that will help us with
23
00:01:30,270 --> 00:01:31,110
our task.
24
00:01:31,170 --> 00:01:34,290
We will call this variable, as we called it previously.
25
00:01:34,320 --> 00:01:35,670
We will call it temp.
26
00:01:35,790 --> 00:01:40,470
And it will be of the same type as the variables A and B..
27
00:01:40,560 --> 00:01:42,600
And in this case, it's integer.
28
00:01:42,750 --> 00:01:49,830
And this temp variable will actually help us here, just like it helped us in the previous example with
29
00:01:49,830 --> 00:01:52,710
the shoes, the swapping general.
30
00:01:52,830 --> 00:01:59,130
So before we do any swap operation, let's first of all print the values of variable A and variable
31
00:01:59,130 --> 00:02:00,060
beat the screen.
32
00:02:00,120 --> 00:02:06,210
This way, once we are done using our swap operation, we can see on our little console application
33
00:02:06,540 --> 00:02:11,790
the results, the the actual values of variable A and B before the swap.
34
00:02:11,820 --> 00:02:16,260
And after the swap, just to make sure that everything was was OK.
35
00:02:16,440 --> 00:02:23,670
And now all that remains is simply to use the similar approach that we've seen in the swap general explanation.
36
00:02:23,940 --> 00:02:27,330
And basically to say that temp will get the value of a.
37
00:02:27,360 --> 00:02:29,000
So temp equals to a.
38
00:02:29,040 --> 00:02:30,000
And there you go.
39
00:02:30,000 --> 00:02:35,160
You can see that ten is now stored also in variable temp.
40
00:02:35,400 --> 00:02:41,790
And then you just write A equals to B and then the variable A. gets the value of B and then you write
41
00:02:41,820 --> 00:02:44,580
B equals to temp in the town.
42
00:02:44,700 --> 00:02:49,940
The value of ten goes from variable, from box temp to variable B.
43
00:02:50,100 --> 00:02:53,760
And now we can say that we are finished with our swap operation.
44
00:02:53,820 --> 00:02:59,210
We can see that variable A contains the value of twenty and variable B contains the value of ten.
45
00:02:59,310 --> 00:03:01,170
Which is exactly as expected.
46
00:03:01,350 --> 00:03:07,290
And what we also would like to do is simply to print the values to the screen to make sure that our
47
00:03:07,290 --> 00:03:09,000
work has succeeded.
48
00:03:09,300 --> 00:03:15,030
So we will just use print F A equals two percentage D and bring the value of variable A..
49
00:03:15,450 --> 00:03:17,220
After this WAP operation.
50
00:03:17,370 --> 00:03:20,130
And we will do also the same for variable B.
51
00:03:20,400 --> 00:03:23,330
So just print F, B equals two the G.
52
00:03:23,610 --> 00:03:26,730
And just print the B value to the screen.
53
00:03:26,880 --> 00:03:33,120
So this way you can make sure that all the operation that you can see the values before this was printed
54
00:03:33,120 --> 00:03:33,720
on the screen.
55
00:03:34,050 --> 00:03:37,170
And also after the swap printed to the screen.
56
00:03:37,290 --> 00:03:39,660
So there shouldn't be any mistakes here.
57
00:03:39,780 --> 00:03:40,410
Right, guys.
58
00:03:40,620 --> 00:03:48,450
But there also may be times where the interviewer will tell you to implement these swap operation things
59
00:03:48,450 --> 00:03:52,510
without using a third variable, without using these stem box.
60
00:03:52,710 --> 00:03:56,460
And that's actually also possible in many ways.
61
00:03:56,580 --> 00:03:59,850
As a matter of fact, bar to live it for another video.
62
00:03:59,870 --> 00:04:06,720
So let me know if you would like me to create a dedicated video just for this topic, how to perform
63
00:04:06,720 --> 00:04:13,320
a swap operation between two variables without using, using, without using guess or a third variable.
64
00:04:14,380 --> 00:04:16,320
I'm tired after this swap thing.
65
00:04:16,710 --> 00:04:19,160
So, as always, thank you guys for watching.
66
00:04:19,410 --> 00:04:21,570
And I'll see you in the next video.
6309
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.