Sub topic 4
Nested IF Statements and Short-Circuit Evaluation
Working assumptions
This chapter assumes that you know basic SQL and the structure of an anonymous PL/SQL block. You should also know how to declare variables, assign values, and run a block with DBMS_OUTPUT enabled.
A nested IF is an IF statement inside another IF statement. Use it when a decision depends on an earlier decision. The inner condition is checked only when execution reaches the inner statement.
DECLARE
l_has_access BOOLEAN := TRUE;
l_is_owner BOOLEAN := FALSE;
l_is_expired BOOLEAN := FALSE;
BEGIN
IF l_has_access THEN
IF l_is_owner THEN
DBMS_OUTPUT.PUT_LINE('Owner access granted');
ELSIF NOT l_is_expired THEN
DBMS_OUTPUT.PUT_LINE('Regular access granted');
ELSE
DBMS_OUTPUT.PUT_LINE('Access expired');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('Access denied');
END IF;
END;
/The outer IF first checks access. Only when l_has_access is TRUE does PL/SQL evaluate the inner conditions. This structure can make dependencies clear, but deeply nested code is difficult to read. Combine related conditions when that produces a simpler rule.
Short-circuit evaluation
PL/SQL evaluates Boolean expressions from left to right and can stop before evaluating every part:
- For
A AND B, ifAisFALSE, PL/SQL does not evaluateB. - For
A OR B, ifAisTRUE, PL/SQL does not evaluateB.
This behavior is called short-circuit evaluation. It is useful for guard conditions. Put the safe or inexpensive test first.
DECLARE
l_name VARCHAR2(30) := NULL;
FUNCTION has_nonempty_name(p_name VARCHAR2) RETURN BOOLEAN IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Name validation was called');
RETURN LENGTH(p_name) > 0;
END;
BEGIN
IF l_name IS NOT NULL AND has_nonempty_name(l_name) THEN
DBMS_OUTPUT.PUT_LINE('Name is usable');
ELSE
DBMS_OUTPUT.PUT_LINE('Name is missing or empty');
END IF;
END;
/Because l_name IS NOT NULL is FALSE, PL/SQL does not call has_nonempty_name. The output is:
Name is missing or emptyDo not rely on short-circuiting to hide unclear business rules. Use it when the order is meaningful, such as checking that a value exists before using it.
NULL in nested conditions
A PL/SQL Boolean can be TRUE, FALSE, or NULL. An IF enters its THEN branch only when the condition is TRUE. Both FALSE and NULL skip that branch.
DECLARE
l_limit NUMBER := NULL;
BEGIN
IF l_limit > 100 THEN
DBMS_OUTPUT.PUT_LINE('Large limit');
ELSE
DBMS_OUTPUT.PUT_LINE('The condition was not TRUE');
END IF;
IF l_limit IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Limit is missing');
END IF;
END;
/Use IS NULL or IS NOT NULL when missing values have a specific meaning. Comparisons such as l_limit > 100 do not return TRUE when l_limit is NULL.
Choosing a structure
Use nested IF statements when the second decision should occur only after the first succeeds. Use AND or OR when the conditions form one rule and short-circuit order improves safety. Keep the condition order visible to the reader.
Exercise
If l_ready is FALSE, will check_details() run in IF l_ready AND check_details() THEN?
Show answer
No. Short-circuit evaluation stops after the FALSE left side of AND.