- Write
if,if…elseandif…else if…elsestatements. - Use the ternary operator for simple choices.
- Explain Solidity’s rules for conditions (boolean only, no switch).
Decision Making
- Decision-making (conditional) statements control the flow of a program: they evaluate a boolean expression and run a block of code only when it is true (or a different block when it is false).
- In Solidity the condition must be of type bool. Unlike C or JavaScript, a number is not automatically treated as true/false – write
if (count != 0), notif (count). - Solidity has no switch/case statement; use an
else ifchain instead. - Braces
{ }may be omitted for a single statement, but using them always is clearer and safer.
if statement
Runs a block only when the condition is true; otherwise the block is skipped.
if (condition) {
// runs when condition is true
}
if…else statement
Chooses between two blocks: one for true, the other for false.
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
if…else if…else statement
Tests several conditions in order; the first true condition’s block runs and the rest are skipped. The final else handles every remaining case.
if (condition1) {
// code
} else if (condition2) {
// code
} else if (condition3) {
// code
} else {
// code when no condition is true
}
Flow of an if…else Statement
boolif blockelse block (if any)Example : Grade Calculator
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract GradeCalculator {
function grade(uint marks) public pure returns (string memory) {
require(marks <= 100, "Marks must be 0-100");
if (marks >= 90) {
return "A+";
} else if (marks >= 75) {
return "A";
} else if (marks >= 60) {
return "B";
} else if (marks >= 40) {
return "C";
} else {
return "Fail";
}
}
function isEven(uint n) public pure returns (bool) {
if (n % 2 == 0) { // condition must be a bool expression
return true;
}
return false;
}
function passStatus(uint marks) public pure returns (string memory) {
return marks >= 40 ? "Pass" : "Fail"; // ternary for a simple choice
}
}
Key Takeaways
- Conditions must be boolean expressions; numbers are not treated as true/false.
- Use an else-if chain for multiple cases – Solidity has no switch statement.
- Order conditions from most specific to least specific; the first true branch wins.
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
if (condition) { /* code to run if condition is true */ }if (condition) { /* true block */ } else { /* false block */ }if (c1) { ... } else if (c2) { ... } else if (c3) { ... } else { ... } – the first true condition’s block runs and the others are skipped.if (count) is a compile error; write if (count != 0).marks >= 40 were tested before marks >= 90, a score of 95 would wrongly receive the lower grade.return marks >= 40 ? "Pass" : "Fail";. Complex logic is clearer with if…else.if (n % 2 == 0) { /* even */ }.if (marks >= 40 && marks < 60) { ... }.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Expressions and Control Structures (if/else, loops, checked arithmetic, error handling, try/catch). Solidity Documentation. https://docs.soliditylang.org/en/latest/control-structures.html
- Solidity Team. (n.d.). Types (value types, reference types, mappings, operators, conversions). Solidity Documentation. https://docs.soliditylang.org/en/latest/types.html
- Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.). Prentice Hall.
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.