Click the blue
Follow us![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/7676dc75-b1bd-4631-9403-15397fc890e8.gif)
C++ Basics 009 – Variables
Variable Assignment and Value Swapping
Learning programming is like playing the game of “organizing stationery” – in C++, a “variable” is like asmall box on your desk. “Assignment” means putting something in the box, and “value swapping” means exchanging the contents of two boxes. Today, we will use “pencil and eraser” as examples, step by step, to ensure you understand!
circle01
First, understand: What is a “variable”?
—— A “small box” in programming
Do you have a box for pencils and a box for erasers on your desk? A “variable” in C++ is just like these boxes:
✦Box name (variable name): For example, the box for pencils is called<span>pencil</span>, and the box for erasers is called<span>eraser</span>, making it easy for you to find it;
✦Box type (data type): A pencil box can only hold pencils, not rulers – variables are the same. For example, a<span>int</span> type box can only hold integers (1, 2, 3…), while a<span>string</span> type box can hold text (like “apple”);
✦Contents of the box (variable value): For example, if there are 5 pencils in the pencil box, then the value of<span>pencil</span> is 5.
✦ For example: If you want to record “you have 3 erasers”, in C++ you would do this to “create a box and put something in it”:
02Variable Assignment: Putting “something” in the box or “changing something”
Assignment is like putting “something” in the box – using the<span>=</span> symbol (note: this is not the “equals sign” in mathematics, but means “put the thing on the right into the box on the left”). There are two cases:
1>>>Initial Assignment: Putting something in the box when creating it
It’s like you just bought a new pencil box and put 4 pencils in it for the first time. In code, you need to “first specify the box type, then give it a name, and finally put something in it”:
![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/109571c1-4760-43b4-9da5-4471e3868f55.png)
Running result:
2>>>Reassignment: Changing the contents of an existing box
If you have 2 pencils left, you need to “dump out” the original 4 pencils in the box and put in 2 pencils – this is “reassignment”, and you don’t need to write int again (because the box has already been created):
![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/de1ac776-76ed-4028-9caa-abbf87238f80.png)
Running result:
3>>>Three small rules for assignment (must remember!)✦ Box types must match:<span>int</span> boxes (for numbers) cannot hold text, for example, <span>int pencil = "pencil"</span> will cause an error (just like a pencil box cannot hold milk);
✦ Variable names cannot be arbitrary: Cannot start with a number (for example, <span>int 1pencil</span> is wrong), and cannot use C++ “keywords” (for example, <span>int int</span> is wrong because <span>int</span> is a type name);
✦ Assignment direction cannot be reversed: You can only “put the thing on the right into the box on the left”, for example, <span>3 = pencil</span> is wrong (you cannot put “3 pencils” into “number 3”).
03Value Swapping: Letting two boxes “exchange contents”
For example: Xiao Ming has 3 pencils (<span>pencilA = 3</span>), and Xiao Hong has 5 pencils (<span>pencilB = 5</span>), and they want to swap pencils – how can this be implemented in code?
01First, a pitfall: Incorrect swapping method (many beginners make this mistake!)
Some might think: directly put the 5 from <span>pencilB</span> into <span>pencilA</span>, and then put the 5 from <span>pencilA</span> into <span>pencilB</span>? Let’s try the code:
![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/2f0bcdb6-2814-4e3b-8e20-329a80847d33.png)
Why is this wrong?It’s like you throw away Xiao Ming’s 3 pencils, directly give Xiao Hong’s 5 pencils to Xiao Ming, and then take Xiao Ming’s 5 pencils to give to Xiao Hong – in the end, both have 5 pencils, and the original 3 pencils are lost!
02Correct method: Use a “temporary box” to store contents (key!)The solution is simple: find a “temporary small box” (temporary variable), first store Xiao Ming’s 3 pencils in it, then swap – just like when you exchange toys, you first put the toys in a temporary basket:
Step breakdown (like playing with building blocks)
✦Prepare 3 boxes:<span><span>pencilA=3</span></span> (Xiao Ming), <span><span>pencilB=5</span></span> (Xiao Hong), <span><span>temp</span></span> (temporary box, empty);
✦First, put Xiao Ming’s 3 pencils into the temporary box:<span><span>temp = pencilA</span></span> (now <span><span>temp=3</span></span>);
✦Put Xiao Hong’s 5 pencils into Xiao Ming’s box:<span><span>pencilA = pencilB</span></span> (now <span><span>pencilA=5</span></span>);
✦Put the 3 pencils from the temporary box into Xiao Hong’s box:<span><span>pencilB = temp</span></span> (now <span><span>pencilB=3</span></span>);
✦Swapping complete! Xiao Ming has 5 pencils, Xiao Hong has 3 pencils.
![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/58c292e7-3475-491e-b9ba-cce5b4411321.png)
Running result:
03Small exercise, try it yourself!
✦ Create two <span>int</span> variables: <span>apple=2</span> (you have 2 apples), <span>orange=4</span> (you have 4 oranges);
✦ Use a temporary variable to swap their values;
✦ Print the results before and after swapping, to see if “apples = 4, oranges = 2”.
(The answer is at the end, write it yourself first!)04Summary: The “core mantra” of variable assignment and swapping01Variable = small box:
First define the type (what to hold), then name it (what to call it), and finally put something in it (assignment);
02Assignment = putting something in:
<span>=</span> means “put right into left”, putting the thing on the right into the box on the left;
03Variable = small box:
First store, then swap, and finally retrieve (temp stores A, A swaps with B, B retrieves temp).
05Reference for exercise answers![C++ Basics 009 [Notes Version - Variable Assignment and Value Swapping]](https://boardor.com/wp-content/uploads/2025/11/3e5e4de9-f9cd-4aee-85a4-c24bcf10c132.png)
Youth Programming StudioWeChat ID: yy18337897171
Focus on children’s growth
Cultivate innovative abilities
C++ Basics 001 [Notes Version]C++ Basics 002 [Notes Version]C++ Basics 003 [Notes Version – Summary of Operators]C++ Basics 004 [Notes Version – Common Data Types]C++ Basics 005 [Notes] Common Mathematical Library FunctionsC++ Basics 006 [Notes Version – Three Major Structures]C++ Basics 007 [Notes Version – Data Type Conversion]C++ Basics 008 [Notes Version – Nested Loops]