Control Structures:
Frequently it is necessary to take another action depending on circumstances. The IF statement permits the execution of a series of statements conditionally. An execution of the program purely depends on the value of the condition specified. A working of IF conditions is similar in other programming languages. There are three categories of IF statements:
IF...THEN
IF..THEN..ELSE
IF..THEN..ELSEIF
IF-THEN
This is the easiest form of the IF statement. This relates a condition with a sequence of statements enclosed through the keywords THEN and END IF. The given example describes this:
Syntax
IF condition THEN
Sequence_of_statements;
End if;
The sequence of statements is executed only if the condition becomes TRUE. In the other case like If the condition yields FALSE or NULL, the IF statement does nothing. Either case, control passes to the next statement subsequent END IF. An instance follows
Example
DECLARE
s number;
BEGIN
S:=&a;
If s>=10 THEN
DBMS_OUTPUT.PUT_LINE('S greater than or equal to 10');
END IF; END;
The given example displays the value 'S greater than or equal to 10' only if the variable s contains the value 10 or greater than that.
IF-THEN-ELSE
Second form of IF statement adds the keyword ELSE followed by an alternative sequence of statements. The common syntax is as follows.
IF condition THEN
Sequence_of_statements1;
ELSE
Sequence_of_statements2;
END IF;
The sequence of statements in the ELSE clause is executed only when if the condition become FALSE or NULL. Therefore, the ELSE clause ensures in which a sequence of statements is executed.