Are conditions in an if statement always checked

Hi there, I’m just wondering if conditions in a if statement are checked in a lazy fashion. See the following snippet for an example. By saying lazy, I mean since in the if statement below, once a true is obtained, for example, if x > 10 is true, the entire condition section must be true, and the other two conditions do not need to be checked at all. So my question is if the other two conditions will be checked regardless of the outcome of the first. Thanks.

function checkConditions(uint256 x, uint256 y, uint256 z) public pure returns (bool) {
    if (x > 10 || y < 230 || z > 56) revert InvalidValues(x, y, z);

    // Do Sth.
}

@hrkrshnn Could you please take a look at this question as well? This is a question I do not know how to do a test to get an answer.

Yeah, Solidity does have short-circuiting for boolean operators.
Yul (the inline assembly language) does not.

It is documented here: Types — Solidity 0.8.7 documentation

As @cameel notes, this is not a property of if, it is a property of the && and || operators. So if you write bool x = f() && g(), g will only be called if f() returns true. Same for ||: In bool x = f() || g(), g() will only be called if f() returns false.

2 Likes

@cameel @chriseth Thanks guys. This can help estimate gas costs better. Really appreciate the clarifications.