If-else statements in Blockchain / Solidity - Time & Space Complexity
We want to see how the time it takes to run if-else statements changes as the input changes in blockchain code.
Are these statements making the program slower when inputs grow?
Analyze the time complexity of the following code snippet.
function checkBalance(address user) {
if (balanceOf(user) > 1000) {
return "Rich";
} else {
return "Not rich";
}
}
This code checks if a user's balance is greater than 1000 and returns a message based on that.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per function call.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: Exactly one check per function call, independent of input size, so operations remain constant.
Time Complexity: O(1)
This means the time remains constant regardless of the input size.
[X] Wrong: "If-else statements scale linearly with input size like O(n)."
[OK] Correct: There are no loops or repetitions; the if-else executes exactly once per call, so it's O(1).
Understanding how simple decisions add up helps you explain code efficiency clearly and confidently.
"What if we added a loop inside the if-else? How would the time complexity change?"
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
