If statement
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
Case:
SET SERVEROUTPUT ON
DECLARE
v_grade CHAR(1) := UPPER(’&p_grade’);
v_appraisal VARCHAR2(20);
BEGIN
v_appraisal :=
CASE v_grade
WHEN ‘A’ THEN ‘Excellent’
WHEN ‘B’ THEN ‘Very Good’
WHEN ‘C’ THEN ‘Good’
ELSE ‘No such grade’
END;
DBMS_OUTPUT.PUT_LINE (’Grade: ‘|| v_grade || ‘
Appraisal ‘ || v_appraisal);
END;
Iterative Control: LOOPStatements
• Loops repeat a statement or sequence of statements multiple times.
• There are three loop types:
– Basic loop
– FORloop
– WHILEloop
LOOP
statement1;
. . .
EXIT [WHEN condition];
END LOOP;
WHILE condition LOOP
statement1;
statement2;
. . .
END LOOP;
FOR counterIN [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
Use a FORloop to shortcut the test for the number of iterations.
• Do not declare the counter; it is declared implicitly.
• ‘lower_bound .. upper_bound’ is required syntax.
Guidelines While Using Loops
• Use the basic loop when the statements inside the loop must execute at least once.
• Use the WHILEloop if the condition has to be evaluated at the start of each iteration.
• Use a FORloop if the number of iterations is known.