CONTINUE statement
Introduce in Oracle 11g,this statement provides an easy way to force an immediate jump to the loop control statement
Let us see this into action by writing a simple Even..Odd number program
SQL> BEGIN
2 FOR i IN 1 .. 10 LOOP
3 IF MOD(i,2) = 0 THEN
4 CONTINUE;
5 DBMS_OUTPUT.PUT_LINE('Even Numbers := ' || TO_CHAR(i));
6 END IF;
7 DBMS_OUTPUT.PUT_LINE('Odd Numbers := ' || TO_CHAR(i));
8 END LOOP;
9 END;
10 /
Odd Numbers := 1
Even Numbers := 2
Odd Numbers := 3
Even Numbers := 4
Odd Numbers := 5
Even Numbers := 6
Odd Numbers := 7
Even Numbers := 8
Odd Numbers := 9
Even Numbers := 10
PL/SQL procedure successfully completed.
In this first case, that i value is 1. So the condition
MOD(i,2) = 0
evaluates to false and the control comes to line #7 and the to line #2. In the second case, the condition
becomes satisfied and the control executes the line # blocks from 4 to 6. After that it reaches to line #2 and the program continues in this manner
CONTINUE-WHEN statement
Syntax
Continue-When(Condition)
Once the condition in the When clause is evaluated and if found true, the current iteration of the loop completes and control passes to the next iteration.
Let us see into action
SQL> BEGIN
2 FOR i IN 1 .. 10 LOOP
3 CONTINUE WHEN MOD(i,2) = 0;
4 DBMS_OUTPUT.PUT_LINE('Numbers := ' || TO_CHAR(i));
5 END LOOP;
6 END;
7 /
Numbers := 1
Numbers := 3
Numbers := 5
Numbers := 7
Numbers := 9
PL/SQL procedure successfully completed.
We can figure out that the even numbers trigger the control to the beginning of the loop.
The CONTINUE-WHEN statement can also be used with LOOP LABELS as shown under
SQL> BEGIN
2 <<outer_loop>>
3 FOR i IN 1 .. 10 LOOP
4 CONTINUE outer_loop WHEN MOD(i,2) = 0;
5 DBMS_OUTPUT.PUT_LINE('Numbers := ' || TO_CHAR(i));
6 END LOOP;
7 END;
8 /
Numbers := 1
Numbers := 3
Numbers := 5
Numbers := 7
Numbers := 9
PL/SQL procedure successfully completed.
We have the Continue statement in Sql Server too about which u can read from
- SQL SERVER – Simple Example of WHILE Loop with BREAK and CONTINUE
- SQL SERVER – Simple Example of WHILE Loop With CONTINUE and BREAK Keywords
Hope this was useful.Thanks for reading