Control Statements in PL/SQL
Control statements in PL/SQL determine the logical flow of a program. There are three main types:
1. Conditional Control: IF Statements
These are used to make decisions based on certain conditions.
Syntax:
IF condition THEN
-- statements
ELSIF another_condition THEN
-- statements
ELSE
-- statements
END IF;
Example:
IF salary > 10000 THEN
DBMS_OUTPUT.PUT_LINE('High salary');
ELSE
DBMS_OUTPUT.PUT_LINE('Low salary');
END IF;
2. Iterative Control: Loops
PL/SQL supports three types of loops:
a. Basic LOOP
LOOP
-- statements
EXIT WHEN condition;
END LOOP;
b. WHILE LOOP
WHILE condition LOOP
-- statements
END LOOP;
c. FOR LOOP
FOR counter IN [REVERSE] start..end LOOP
-- statements
END LOOP;
Example:
FOR i IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
3. Sequential Control: GOTO Statement
Used to jump to a labeled statement. Avoided in practice for readability.
Example:
BEGIN
GOTO skip;
DBMS_OUTPUT.PUT_LINE('This line is skipped');
<<skip>>
DBMS_OUTPUT.PUT_LINE('Jumped here');
END;