In Oracle Database 11g PL/SQL has a new construct called CONTINUE , which is used in a loop. The statement moves the logic to the end of the loop and then to the beginning of the loop.The CONTINUE statement jumps out of the current loop iteration and starts the next one. It can be used on its own, or as part of a CONTINUE WHEN statement. This type of processing has always been possible using IF statements either on their own or with exceptions or GOTO statements, but the CONTINUE statement is neater and brings PL/SQL in line with other languages.
Sample Code :
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
2 l_itr NUMBER := 0;
3 BEGIN
4 FOR i IN 1 .. 100 LOOP
5 CONTINUE WHEN MOD(i,5) = 0;
6 l_itr := l_itr + 1;
7 END LOOP;
8
9 DBMS_OUTPUT.put_line('Loop Interations Using CONTINUE WHEN : ' || l_itr);
10
11 l_itr := 0;
12
13 FOR i IN 1 .. 100 LOOP
14 IF MOD(i,5) = 0 THEN
15 CONTINUE;
16 END IF;
17 l_itr := l_itr + 1;
18 END LOOP;
19
20 DBMS_OUTPUT.put_line('Loop Iterations using IF..CONTINUE ' || l_itr);
21 END;
22 /
Loop Interations Using CONTINUE WHEN : 80
Loop Iterations using IF..CONTINUE 80
PL/SQL procedure successfully completed.
SQL>
SQL> DECLARE
2 l_itr NUMBER := 0;
3 BEGIN
4 FOR i IN 1 .. 100 LOOP
5 CONTINUE WHEN MOD(i,5) = 0;
6 l_itr := l_itr + 1;
7 END LOOP;
8
9 DBMS_OUTPUT.put_line('Loop Interations Using CONTINUE WHEN : ' || l_itr);
10
11 l_itr := 0;
12
13 FOR i IN 1 .. 100 LOOP
14 IF MOD(i,5) = 0 THEN
15 CONTINUE;
16 END IF;
17 l_itr := l_itr + 1;
18 END LOOP;
19
20 DBMS_OUTPUT.put_line('Loop Iterations using IF..CONTINUE ' || l_itr);
21 END;
22 /
Loop Interations Using CONTINUE WHEN : 80
Loop Iterations using IF..CONTINUE 80
PL/SQL procedure successfully completed.
SQL>
The execution of the loop was skipped 20 times using CONTINUE statement and hence the loop iterated only 80 times.
Comments
Post a Comment