Learning Objectives – after this chapter you will be able to:
  • Write if, if…else and if…else if…else statements.
  • 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), not if (count).
  • Solidity has no switch/case statement; use an else if chain 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
Evaluate conditionExpression must be bool
trueRun the if block
falseRun the else block (if any)
ContinueExecution continues after the statement
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.
Download | Print      E-Notes

The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.

Video : Decision Making (if, else if, else)
Resource File
Question / Answer (15 questions)

Answer : They are conditional statements that evaluate a boolean expression and decide which block of code to execute, controlling the flow of a program.

Answer : if (condition) { /* code to run if condition is true */ }

Answer : if (condition) { /* true block */ } else { /* false block */ }

Answer : if (c1) { ... } else if (c2) { ... } else if (c3) { ... } else { ... } – the first true condition’s block runs and the others are skipped.

Answer : Yes. Solidity does not convert numbers to booleans automatically, so if (count) is a compile error; write if (count != 0).

Answer : No. Multiple cases are handled with an if…else if…else chain.

Answer : Only the block of the first true condition runs; the remaining conditions are not even evaluated.

Answer : Because the first true branch wins. If marks >= 40 were tested before marks >= 90, a score of 95 would wrongly receive the lower grade.

Answer : For a simple choice between two values in a single expression, e.g. return marks >= 40 ? "Pass" : "Fail";. Complex logic is clearer with if…else.

Answer : No, they can be omitted for one statement, but always using braces makes code clearer and prevents mistakes when more statements are added later.

Answer : It validates the input before the decision logic runs; if marks exceed 100 the call reverts with the message “Marks must be 0-100”.

Answer : Use the modulus operator: if (n % 2 == 0) { /* even */ }.

Answer : It handles every case not matched by the previous conditions, acting as a default branch.

Answer : Yes, an if or else block can contain another if statement, although deep nesting reduces readability and is often replaced with else-if chains or early returns.

Answer : The AND operator: 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.

Q1. Which of these conditions is VALID in Solidity?

Show Answer

Correct Answer : B. if (count != 0)
Conditions must be boolean expressions.

Q2. Solidity’s replacement for a switch statement is:

Show Answer

Correct Answer : C. an if…else if…else chain
There is no switch; use else-if chains.

Q3. With the GradeCalculator, grade(80) returns:

Show Answer

Correct Answer : A. A
80 is not ≥ 90 but is ≥ 75, so "A".

Q4. In an if…else if chain, how many blocks run at most?

Show Answer

Correct Answer : D. One
Only the first true branch runs.

Q5. marks >= 40 ? "Pass" : "Fail" with marks = 35 returns:

Show Answer

Correct Answer : A. "Fail"
35 is less than 40, so the false value is returned.

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.

  1. 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
  2. Solidity Team. (n.d.). Types (value types, reference types, mappings, operators, conversions). Solidity Documentation. https://docs.soliditylang.org/en/latest/types.html
  3. Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.). Prentice Hall.
  4. 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.