Short circuiting logical operations

  1. 4 months ago

    Marc

    Jun 20 User since 2018

    Example:

    var x = 1
    var y = 0
    
    if (y == 0 or x / y == 1) {
    }

    In this example we have 2 expressions in the if-condition using "or" as the logical operator. If the first expression is true, then the second one doesn't have to be evaluated. In this case it *is* evaluated, resulting in a "Division by 0 error".

    The only work-around is using 2 nested if statements.

    NB: In this example I'm using a division expression, but it's also suitable for many other expressions.

or Sign Up to reply!