If/Else

# If/Else

Solidity suporta estruturas condicionais if, else if e else.

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

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

    function ternary(uint _x) public pure returns (uint) {
        // if (_x < 10) {
        //     retorna 1;
        // }
        // retorna 2;

        // um forma abreviada de escrever a instrução if / else
        // é usando o operador "?" ele é chamado de operador ternário
        return _x < 10 ? 1 : 2;
    }
}

# Teste no Remix

Last Updated: 22/01/2024 22:26:13