Two numbers are stored in a and b.
You want to swap them.
How can you ask CPU do the job?
First, you need an extra variable, temp to hold data.
Then, we start the procedure.
1. Let temp hold the number of a. That is, write the number of a into temp.
temp = a; |
2. Now, a is free to take the number of b. That is, write the number of b into a.
a = b; |
3. Now, b is free to take the number of temp. That is, write the number of temp into b.
b = temp; |