0
0
Blockchain / Solidityprogramming~10 mins

Mappings in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mappings
Start
Define mapping
Add key-value pair
Access value by key
Update or delete value
End
This flow shows how a mapping is defined, used to store key-value pairs, accessed, updated, and deleted in blockchain smart contracts.
Execution Sample
Blockchain / Solidity
mapping(address => uint) balances;

function updateBalance(address user, uint amount) public {
  balances[user] = amount;
}

function getBalance(address user) public view returns (uint) {
  return balances[user];
}

function deleteBalance(address user) public {
  delete balances[user];
}
This code defines a mapping from addresses to numbers, updates a user's balance, retrieves it, and deletes a balance.
Execution Table
StepActionKey (address)Value (uint)Mapping StateOutput
1Define empty mapping--{}-
2Call updateBalance(user1, 100)user1100{user1: 100}-
3Call updateBalance(user2, 50)user250{user1: 100, user2: 50}-
4Call getBalance(user1)user1-{user1: 100, user2: 50}100
5Call updateBalance(user1, 200)user1200{user1: 200, user2: 50}-
6Call getBalance(user1)user1-{user1: 200, user2: 50}200
7Call getBalance(user3) (not set)user3-{user1: 200, user2: 50}0
8Delete balances[user2]user2-{user1: 200}-
9Call getBalance(user2)user2-{user1: 200}0
💡 Execution ends after accessing and updating mapping entries and deleting a key.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 8Final
balances{}{user1: 100}{user1: 100, user2: 50}{user1: 200, user2: 50}{user1: 200}{user1: 200}
Key Moments - 3 Insights
Why does getBalance(user3) return 0 even though user3 was never set?
Mappings in blockchain return the default value for the value type if the key does not exist. Here, uint default is 0, as shown in execution_table row 7.
What happens when we update balances[user1] from 100 to 200?
The value for key user1 is overwritten with the new value 200, as seen in execution_table row 5 and variable_tracker after step 5.
How does deleting a key affect the mapping?
Deleting a key removes its entry, so accessing it returns the default value 0, shown in execution_table rows 8 and 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of balances[user1] after step 3?
A50
B100
C200
D0
💡 Hint
Check the Mapping State column at step 3 in the execution_table.
At which step does balances[user1] get updated to 200?
AStep 5
BStep 7
CStep 2
DStep 8
💡 Hint
Look for the updateBalance call that changes user1's value in the execution_table.
If we delete balances[user2], what will getBalance(user2) return?
A50
BUndefined
C0
DError
💡 Hint
Refer to execution_table rows 8 and 9 showing deletion and access after deletion.
Concept Snapshot
Mappings store key-value pairs like a dictionary.
Keys map to values; if key missing, returns default value.
Use mapping(keyType => valueType) syntax.
Update by assigning new value to a key.
Delete keys to reset to default.
Common in blockchain for balances and states.
Full Transcript
Mappings in blockchain smart contracts are like special dictionaries that connect keys to values. You define a mapping with a key type and a value type. When you add or update a key, the mapping stores the value. If you ask for a key that was never set, it returns the default value for that type, like 0 for numbers. You can also delete keys to remove their stored value. This example showed how balances are stored for addresses, updated, accessed, and deleted step-by-step.