logo

Solidity by Example
version 0.8.3

If / Else

Solidity support conditional statements if, else if and else.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract IfElse {
    function foo(uint x) public pure returns (uint) {
        if (x < 10) {
            return 0;
        } else if (x < 20) {
            return 1;
        } else {
            return 2;
        }
    }
}

Try on Remix

Take a course at Smart Contract Engineer