0
0
Blockchain / Solidityprogramming~10 mins

If-else statements in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Blockchain / Solidity
if (balance >= amount) {
  send(amount);
} else {
  revert("Insufficient funds");
}
Checks if balance is enough to send amount; sends if yes, else stops with error.
Execution Table
StepCondition (balance >= amount)ResultBranch TakenAction
1balance=100, amount=50TrueIfsend(50) called
2balance=30, amount=50FalseElserevert("Insufficient funds") called
💡 Execution stops after either sending amount or reverting due to insufficient funds.
Variable Tracker
VariableStartAfter Step 1After Step 2
balance10010030
amount505050
Key Moments - 2 Insights
Why does the code run the else block when balance is less than amount?
Because the condition balance >= amount is false (see execution_table row 2), so the else branch runs.
What happens if the condition is true?
The if block runs and the send function is called (see execution_table row 1). The else block is skipped.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action happens when balance is 100 and amount is 50?
ANothing happens
Brevert("Insufficient funds") is called
Csend(50) is called
DBoth send and revert are called
💡 Hint
Check execution_table row 1 where condition is true and if branch runs.
At which step does the condition become false?
AStep 2
BStep 1
CNever
DBoth steps
💡 Hint
Look at execution_table row 2 where condition is false.
If amount was 20 instead of 50 at step 2, what would happen?
AElse branch runs
BIf branch runs
CCode reverts anyway
DNo action taken
💡 Hint
Compare balance and amount in variable_tracker and condition in execution_table.
Concept Snapshot
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 { ... }
Full Transcript
This visual trace shows how if-else statements work in blockchain programming. The program checks if balance is enough to send an amount. If yes, it calls send(amount). If no, it calls revert with an error message. The execution table shows two steps: one where the condition is true and send is called, and one where the condition is false and revert is called. Variables balance and amount are tracked to see their values at each step. Key moments clarify why the else block runs when the condition is false and what happens when it is true. The quiz tests understanding of which branch runs based on the condition. The snapshot summarizes the if-else concept simply.