What if your blockchain could instantly decide what to do next without you lifting a finger?
Why If-else statements in Blockchain / Solidity? - Purpose & Use Cases
Imagine you are manually checking each transaction on a blockchain to decide if it should be approved or rejected based on certain rules.
You write down every condition on paper and then try to apply them one by one for each transaction.
This manual checking is slow and tiring.
It’s easy to forget a rule or make mistakes when handling many transactions.
Also, you cannot quickly change the rules or handle new cases without rewriting everything.
If-else statements let you write clear rules in code that automatically check conditions and decide what to do next.
This makes your blockchain program smart and fast, handling many cases without errors.
if transaction_amount > 1000: approve else: reject
if transaction_amount > 1000: approve() else: reject()
If-else statements enable your blockchain code to make decisions automatically, adapting to different situations instantly.
In a blockchain voting system, if a user’s vote is valid and within the deadline, the vote is counted; otherwise, it is ignored.
If-else statements automate decision-making in code.
They reduce errors and speed up processing.
They make blockchain programs flexible and reliable.
Practice
What does an if-else statement do in a blockchain smart contract?
Solution
Step 1: Understand the role of if-else
An if-else statement checks a condition and decides which code to run next.Step 2: Apply to blockchain context
In smart contracts, it helps decide actions like approving transactions or rejecting them.Final Answer:
It chooses between two actions based on a condition. -> Option AQuick Check:
If-else = choose action [OK]
- Confusing if-else with loops
- Thinking if-else stores data
- Believing if-else creates blockchains
Which of the following is the correct syntax for an if-else statement in Solidity?
uint balance = 100;
Solution
Step 1: Recall Solidity if-else syntax
Solidity uses parentheses around conditions and braces for code blocks without 'then'.Step 2: Compare options
Only if (balance > 50) { /* code */ } else { /* code */ } matches Solidity syntax:if (condition) { } else { }.Final Answer:
if (balance > 50) { /* code */ } else { /* code */ } -> Option DQuick Check:
Solidity if-else = parentheses + braces [OK]
- Using 'then' keyword
- Missing parentheses around condition
- Using colons instead of braces
What will be the output of this Solidity function when amount = 150?
function checkAmount(uint amount) public pure returns (string memory) {
if (amount > 100) {
return "Large amount";
} else {
return "Small amount";
}
}Solution
Step 1: Check the condition with amount = 150
Since 150 > 100, the if condition is true.Step 2: Determine which return runs
The function returns "Large amount" because the if branch runs.Final Answer:
"Large amount" -> Option CQuick Check:
150 > 100 -> "Large amount" [OK]
- Confusing greater than with less than
- Assuming else runs when if is true
- Expecting syntax errors
Find the error in this Solidity snippet:
function checkBalance(uint balance) public pure returns (string memory) {
if balance > 0 {
return "Positive";
} else {
return "Zero or Negative";
}
}Solution
Step 1: Check if statement syntax
In Solidity, the condition must be inside parentheses:if (condition).Step 2: Identify the error
The code hasif balance > 0without parentheses, causing a syntax error.Final Answer:
Missing parentheses around the if condition -> Option AQuick Check:
if condition needs parentheses [OK]
- Omitting parentheses in if condition
- Thinking semicolons are missing after return
- Confusing return type errors
You want to write a Solidity function that returns "Access granted" only if userRole is exactly "admin", otherwise returns "Access denied". Which code snippet correctly uses if-else to do this?
Solution
Step 1: Understand string comparison in Solidity
Solidity uses==to compare strings for equality, not=or methods likeequals().Step 2: Check syntax correctness
if (userRole == "admin") { return "Access granted"; } else { return "Access denied"; } uses==with parentheses and braces correctly.Final Answer:
if (userRole == "admin") { return "Access granted"; } else { return "Access denied"; } -> Option BQuick Check:
Use == for string comparison in if [OK]
- Using single = instead of ==
- Omitting parentheses in if
- Using Java-style equals() method
Ready to prove it?
Pass the test and this pattern moves unknown
