Quick Search:
 
 The Oracle PL/SQL IF Keyword      [Return To Index] Jump to:  

Term: IF

Definition:
The Oracle IF keyword is part of the PL/SQL IF...THEN construct which implements conditional control in PL/SQL. Conditional Control refers to taking an alternative action instead of regular one, based on defined conditions or logic-based decisions. It allows conditional execution of statements in defined sequence. The IF...THEN construct is available in three formats:

  1. IF...THEN
  2. IF...THEN...ELSE
  3. IF...THEN...ELSIF...ENDIF

IF THEN...END IF statement is a single-level conditional statement which executes a sequence of statements based on a given condition:

IF condition THEN
executable statements
END IF;


A plain-English example of this could be, "IF the temperature is below 50 degrees, THEN turn on the heater."

The IF-THEN-ELSE statement is next level of extension of above the IF-THEN statement, as it provides the ability to give direction for FALSE or NULL also.

IF condition THEN
executable statements 1
ELSE
executable statements 2
END IF;


A plain-English example of this could be, "IF the temperature is below 50 degrees, THEN turn on the heater, ELSE open a window."


For multiple possible outcomes of a condition, we use the IF THEN ELSIF ENDIF statement.

IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;


Example Usage:

The block of code below looks at the value of the variable 'V_GRADE'. If it is 'P' then it prints out 'PASS'.

DECLARE
V_GRADE VARCHAR2(1) := 'P';
BEGIN
IF V_GRADE = 'P' THEN
DBMS_OUTPUT.PUT_LINE('PASS');
END IF;
END;



Related Links:

Related Code Snippets:
 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org