If-else statements help your program make choices. They let your code do different things depending on conditions.
If-else statements in Blockchain / Solidity
if (condition) { // code to run if condition is true } else { // code to run if condition is false }
condition is a check that results in true or false.{ } to group the code blocks clearly.if (balance >= price) { approvePurchase(); } else { rejectPurchase(); }
if (isOwner) { allowAccess(); } else { denyAccess(); }
This smart contract checks if the balance is enough to buy an item. The buy function returns a message based on the check.
pragma solidity ^0.8.0; contract SimpleCheck { uint public balance = 100; uint public price = 50; function buy() public view returns (string memory) { if (balance >= price) { return "Purchase approved"; } else { return "Purchase denied"; } } }
In blockchain smart contracts, if-else helps control what actions happen based on conditions.
Always test your conditions carefully to avoid unexpected results.
If-else lets your code choose between two paths.
Use it to check conditions and run different code accordingly.
It is very useful in blockchain for controlling transactions and permissions.
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
