Oracle PL/SQL - Short-Circuit Evaluation

Introduction

When evaluating a logical expression, PL/SQL uses short-circuit evaluation.

PL/SQL stops evaluating the expression as soon as it can determine the result.

In the following code, short-circuit evaluation prevents the OR expression from causing a divide-by-zero error.

When the value of zero_value is zero, the value of the left operand is TRUE, so PL/SQL does not evaluate the right operand.

If PL/SQL evaluated both operands before applying the OR operator, the right operand would cause a division by zero error.

Demo

SQL>
SQL>--   w w w.jav  a  2  s  .  c o m
SQL> DECLARE
  2    zero_value  INTEGER := 0;
  3    total INTEGER := 100;
  4  BEGIN
  5    IF  (zero_value = 0) OR ((total / zero_value) < 5) THEN
  6      DBMS_OUTPUT.PUT_LINE('On hand quantity is zero.');
  7    END IF;
  8  END;
  9  /
On hand quantity is zero.

PL/SQL procedure successfully completed.

SQL>
SQL>

Related Topic