Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Statement
|
Description
|
IF - THEN statement
|
The IF statement associates a condition with a sequence of statements enclosed by the keywords THEN and END IF. If the condition is true, the statements get executed and if the condition is false or NULL then the IF statement does nothing.
|
IF-THEN-ELSE statement
|
IF statement adds the keyword ELSE followed by an alternative sequence of statement. If the condition is false or NULL , then only the alternative sequence of statements get executed. It ensures that either of the sequence of statements is executed.
|
IF-THEN-ELSIF statement
|
It allows you to choose between several alternatives.
|
Case statement
|
Like the IF statement, the CASE statement selects one sequence of statements to execute. However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean expressions. A selector is an expression whose value is used to select one of several alternatives.
|
Searched CASE statement
|
The searched CASE statement has no selector, and it's WHEN clauses contain search conditions that yield Boolean values.
|
nested IF-THEN-ELSE
|
You can use one IF-THEN or IF-THEN-ELSIFstatement inside another IF-THEN or IF-THEN-ELSIF statement(s).
|
Example 1:
a NUMBER (2) := 10;
BEGIN
a := 10; -- check the boolean condition using if statement
IF (a < 20)
THEN -- if condition is true then print the following
DBMS_OUTPUT.put_line ('a is less than 20 ');
END IF;
DBMS_OUTPUT.put_line ('value of a is : ' || a);
END;
Example 2:
DECLARE
a number(3) := 100;
BEGIN
-- check the boolean condition using if statement
IF( a < 20 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
ELSE
dbms_output.put_line('a is not less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
Example 3:
DECLARE
a NUMBER (3) := 100;
BEGIN
IF (a = 10)
THEN
DBMS_OUTPUT.put_line ('Value of a is 10');
ELSIF (a = 20)
THEN
DBMS_OUTPUT.put_line ('Value of a is 20');
ELSIF (a = 30)
THEN
DBMS_OUTPUT.put_line ('Value of a is 30');
ELSE
DBMS_OUTPUT.put_line ('None of the values is matching');
END IF;
END;
Example 4:
Example 4:
DECLARE
a NUMBER (3) := 100;
b NUMBER (3) := 200;
BEGIN
-- check the boolean condition
IF (a = 100)
THEN
-- if condition is true then check the following
IF (b = 200)
THEN
-- if condition is true then print the following
DBMS_OUTPUT.put_line ('Value of a is 100 and b is 200');
END IF;
END IF;
DBMS_OUTPUT.put_line ('Exact value of a is : ' || a);
DBMS_OUTPUT.put_line ('Exact value of b is : ' || b);
END;
GOTO statement: A GOTO statement in PL/SQL programming language provides an unconditional jump from the GOTO to a labeled statement in the same subprogram.
GOTO statement: A GOTO statement in PL/SQL programming language provides an unconditional jump from the GOTO to a labeled statement in the same subprogram.
DECLARE
a NUMBER (2) := 10;
BEGIN
<<loopstart>>
-- while loop execution
WHILE a < 20
LOOP
DBMS_OUTPUT.put_line ('value of a: ' || a);
a := a + 1;
IF a = 15
THEN
a := a + 1;
GOTO loopstart;
END IF;
END LOOP;
END;