Concept Flow - If-else statements
Start
Evaluate Condition
Execute If Block
End If-Else
Execute Else Block
End If-Else
The program checks a condition. If true, it runs the 'if' part; if false, it runs the 'else' part.
if (balance >= amount) { send(amount); } else { revert("Insufficient funds"); }
| Step | Condition (balance >= amount) | Result | Branch Taken | Action |
|---|---|---|---|---|
| 1 | balance=100, amount=50 | True | If | send(50) called |
| 2 | balance=30, amount=50 | False | Else | revert("Insufficient funds") called |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| balance | 100 | 100 | 30 |
| amount | 50 | 50 | 50 |
If-else statements check a condition.
If true, run the 'if' block.
If false, run the 'else' block.
Used to control program flow based on conditions.
Syntax: if (condition) { ... } else { ... }What does an if-else statement do in a blockchain smart contract?
Which of the following is the correct syntax for an if-else statement in Solidity?
uint balance = 100;
if (condition) { } else { }.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";
}
}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";
}
}if (condition).if balance > 0 without parentheses, causing a syntax error.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?
== to compare strings for equality, not = or methods like equals().== with parentheses and braces correctly.Pass the test and this pattern moves unknown