//
you're reading...
PL/SQL

PL/SQL New Feature : CONTINUE statement

CONTINUE statement is finally comes with Oracle 11g.

It comes in two forms:

CONTINUE

Continue statement terminates the current iteration of the loop and passes the control to the next iteration of the loop.

 
hr@O11R2> DECLARE
     v_sum NUMBER:=0;
     BEGIN
         FOR i IN 1 .. 10 LOOP
            IF MOD(i,2) = 0 THEN
               CONTINUE;
            END IF;
            DBMS_OUTPUT.PUT_LINE('Odd Numbers := ' || i);
            v_sum:=v_sum+i;
          END LOOP;
         DBMS_OUTPUT.PUT_LINE('Total sum is := ' || v_sum);
       END;
      /

Odd Numbers := 1
Odd Numbers := 3
Odd Numbers := 5
Odd Numbers := 7
Odd Numbers := 9
Total sum is := 25

PL/SQL procedure successfully completed.

NOTE:
CONTINUE statement condition evaluates with the help of IF statement.

CONTINUE WHEN

Same as CONTINUE statement. But the condition in WHEN clause evaluated to TRUE then it cause the current iteration of loop terminates and pass control to next iteration.

 
hr@O11R2> DECLARE
     v_sum NUMBER:=0;
     BEGIN
         FOR i IN 1 .. 10 LOOP
            CONTiNUE WHEN MOD(i,2) = 0;
            DBMS_OUTPUT.PUT_LINE('Odd Numbers := ' || i);
            v_sum:=v_sum+i;
          END LOOP;
         DBMS_OUTPUT.PUT_LINE('Total sum is := ' || v_sum);
       END;
      /

Odd Numbers := 1
Odd Numbers := 3
Odd Numbers := 5
Odd Numbers := 7
Odd Numbers := 9
Total sum is := 25

PL/SQL procedure successfully completed.

Another way to program this code using labeled loop.

hr@O11R2> DECLARE
     v_sum NUMBER:=0;
     BEGIN
       <<cw_loop>>
         FOR i IN 1 .. 10 LOOP
            CONTiNUE cw_loop WHEN MOD(i,2) = 0;
            DBMS_OUTPUT.PUT_LINE('Odd Numbers := ' || i);
            v_sum:=v_sum+i;
          END LOOP;
         DBMS_OUTPUT.PUT_LINE('Total sum is := ' || v_sum);
       END;
      /

Odd Numbers := 1
Odd Numbers := 3
Odd Numbers := 5
Odd Numbers := 7
Odd Numbers := 9
Total sum is := 25

PL/SQL procedure successfully completed.
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: