📖 Scenario: Imagine you are creating a simple blockchain smart contract that controls how money is sent between users. The contract must check if the sender has enough money before allowing the transfer. This is like checking if you have enough cash in your wallet before buying something.
🎯 Goal: You will build a simple smart contract logic that controls the execution of a money transfer based on the sender's balance. This teaches why logic is important to control what happens in blockchain programs.
📋 What You'll Learn
Create a dictionary called balances with exact user balances
Create a variable called transfer_amount with the exact value 50
Write an if statement that checks if the sender has enough balance
Print "Transfer successful" if the transfer can happen, else print "Insufficient balance"
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains use logic to control money transfers and other actions securely.
💼 Career
Understanding how logic controls execution is key for blockchain developers writing safe and correct smart contracts.
Progress0 / 4 steps
1
DATA SETUP: Create user balances
Create a dictionary called balances with these exact entries: 'Alice': 100, 'Bob': 30, 'Charlie': 75
Blockchain / Solidity
Hint
Think of balances as a wallet for each user showing how much money they have.
2
CONFIGURATION: Set the transfer amount
Create a variable called transfer_amount and set it to 50
Blockchain / Solidity
Hint
This is the amount Alice wants to send.
3
CORE LOGIC: Check if transfer can happen
Write an if statement that checks if balances['Alice'] is greater than or equal to transfer_amount
Blockchain / Solidity
Hint
This logic controls if the transfer can happen or not.
4
OUTPUT: Show the transfer result
Run the program to print the transfer result message
Blockchain / Solidity
Hint
You should see the message that tells if the transfer can happen.
Practice
(1/5)
1. Why is logic important in blockchain programming?
easy
A. It creates new cryptocurrencies automatically
B. It stores data permanently on the blockchain
C. It controls which parts of the code run based on conditions
D. It speeds up the blockchain network
Solution
Step 1: Understand the role of logic in code
Logic decides which code runs by checking conditions, like a traffic light controlling cars.
Step 2: Apply this to blockchain context
In blockchain, logic ensures only allowed actions happen, protecting assets and data.
Final Answer:
It controls which parts of the code run based on conditions -> Option C
Quick Check:
Logic controls execution = D [OK]
Hint: Logic decides code flow by conditions, like a gatekeeper [OK]
Common Mistakes:
Confusing logic with data storage
Thinking logic speeds network
Assuming logic creates cryptocurrencies
2. Which of the following is the correct syntax for an if statement in Solidity (blockchain language)?
easy
A. if (balance > 0) { sendFunds(); }
B. if balance > 0 then sendFunds()
C. if balance > 0 { sendFunds(); }
D. if (balance > 0) sendFunds;
Solution
Step 1: Recall Solidity if statement syntax
Solidity uses C-style syntax: condition in parentheses, code block in braces.
Step 2: Check each option
if (balance > 0) { sendFunds(); } matches correct syntax with parentheses and braces; others miss parentheses, braces, or semicolon.
Final Answer:
if (balance > 0) { sendFunds(); } -> Option A
Quick Check:
Solidity if syntax = C [OK]
Hint: If statements need parentheses and braces in Solidity [OK]
Common Mistakes:
Omitting parentheses around condition
Missing braces for code block
Leaving out semicolons
3. What will be the output of this Solidity function when amount = 50?
function checkAmount(uint amount) public pure returns (string memory) {
if (amount > 100) {
return "High";
} else if (amount > 20) {
return "Medium";
} else {
return "Low";
}
}
medium
A. "High"
B. Compilation error
C. "Low"
D. "Medium"
Solution
Step 1: Evaluate the if conditions with amount = 50
50 > 100 is false, so skip first if; 50 > 20 is true, so return "Medium".
Step 2: Confirm no errors in code
Code syntax is correct, so no compilation error.
Final Answer:
"Medium" -> Option D
Quick Check:
50 > 20 = true, output = "Medium" [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
Choosing "High" because 50 is large
Ignoring else if condition order
Thinking code has syntax errors
4. Identify the error in this Solidity code snippet:
function transfer(uint amount) public {
if amount > balance {
revert("Insufficient balance");
}
balance -= amount;
}
medium
A. Missing parentheses around the if condition
B. Using revert instead of throw
C. balance should be a local variable
D. Missing semicolon after revert statement
Solution
Step 1: Check if statement syntax
In Solidity, conditions must be inside parentheses: if (condition) {...}
Step 2: Verify other parts
Using revert is correct for errors; semicolon is present; balance can be state variable.
Final Answer:
Missing parentheses around the if condition -> Option A
Quick Check:
If condition needs parentheses = A [OK]
Hint: If conditions always need parentheses in Solidity [OK]
Common Mistakes:
Thinking revert is wrong
Assuming balance must be local
Missing that parentheses are required
5. You want to write a Solidity function that only allows transfers if the sender is the owner and the amount is less than the balance. Which logic correctly controls execution?
hard
A. if (msg.sender == owner || amount < balance) { transfer(amount); } else { revert("Not allowed"); }
B. if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); }
C. if (msg.sender != owner && amount > balance) { transfer(amount); } else { revert("Not allowed"); }
D. if (msg.sender != owner || amount > balance) { transfer(amount); } else { revert("Not allowed"); }
Solution
Step 1: Understand the required conditions
Both conditions must be true: sender is owner AND amount less than balance.
Step 2: Check each option's logic
if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } uses && (AND) correctly; B uses || (OR) which allows wrong cases; C and D use negations incorrectly.
Final Answer:
if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } -> Option B
Quick Check:
Use AND for both conditions true = A [OK]
Hint: Use AND (&&) to require all conditions true [OK]
Common Mistakes:
Using OR (||) instead of AND (&&)
Negating conditions incorrectly
Allowing transfers when conditions fail
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.