Lesson 6
Lesson 6
2-18
3-23
• Selection statements
Conditional control structures (IF statement)]
• Iteration statements
Loop control structures
– Basic loop
– FOR loop
4-23
– WHILE loop
– EXIT AND CONTINUE statement
.Oracle Corporation, 1996. All rights reserved سCopyright
The IF Statement: Syntax
• You can perform actions selectively based upon
conditions being met.
IF
IF condition
condition THEN
THEN
statements
statements;;
[ELSIF
[ELSIF condition
condition THEN
THEN
statements
statements;]
;]
[ELSE
[ELSE
statements
statements;]
;]
END
END IF;
IF;
6-23
7-23
8-23
IF condition THEN
;statements
;END IF
9-23
10-23
11-23
DECLARE
;n_sales NUMBER := 300000
;n_commission NUMBER( 10, 2 ) := 0
BEGIN
IF n_sales > 200000 THEN
;n_commission := n_sales * 0.1
ELSE
;n_commission := n_sales * 0.05
;END IF
;END
12-23
IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
... ] ELSIF condition_3 THEN statements_3 [
] ELSE else_statements [
;END IF
13-23
IF condition_1 THEN
IF condition_2 THEN
;nested_if_statements
;END IF
ELSE
;else_statements
;END IF
15-23
CASE selector
WHEN selector_value_1 THEN
statements_1
WHEN selector_value_1 THEN
... statement_2
ELSE
else_statements
;END CASE
16-23
17-23
TRUE TRUE FALSE NULL TRUE TRUE TRUE TRUE TRUE FALSE
FALSE FALSE FALSE FALSE FALSE TRUE FALSE NULL FALSE TRUE
NULL NULL FALSE NULL NULL TRUE NULL NULL NULL NULL
18-23
21-23
22-23
25-23
;END LOOP
;end
26-23
.. .. ..
v_ord_id
v_ord_id s_item.ord_id%TYPE
s_item.ord_id%TYPE :=:= 101;
101;
v_counter
v_counter NUMBER(2)
NUMBER(2) :=
:= 1;
1;
BEGIN
BEGIN
.. .. ..
WHILE
WHILE v_counter
v_counter <=
<= 10
10 LOOP
LOOP
INSERT
INSERT INTO
INTO s_item
s_item (ord_id,
(ord_id, item_id)
item_id)
VALUES
VALUES (v_ord_id,
(v_ord_id, v_counter);
v_counter);
v_counter
v_counter :=
:= v_counter
v_counter ++ 1;
1;
END
END LOOP;
LOOP;
.. .. ..
27-23
28-23
begin
for i in 1..10 loop
;DBMS_OUTPUT.PUT_LINE(i)
;end loop
;end
29-23
30-23
31-23
Example
• Print the number of times the loop is executed
and the last value for the index.
Guidelines
• Reference the index within the loop only; it is
undefined outside the loop.
• Use an expression to reference the existing value
of an index.
• Do not reference the index as the target of an
assignment.
32-23
33-23
begin
for i in 1..10 loop
;EXIT WHEN (i = 4)
;DBMS_OUTPUT.PUT_LINE(i)
;end loop
;end
35-23
•
begin •
for i in 1..10 loop •
;CONTINUE WHEN (i = 4) •
;DBMS_OUTPUT.PUT_LINE(i) •
;end loop •
;end •
36-23
37-23
38-23