Database Programming Using Oracle 11g Iterative Control-II
Iterative Control-II Iterative Control-II: Continue Statement Conditionally Exit from current iteration of loop Statements after continue are skipped Control is transfer to next iteration Must be inside loop
Iterative Control-II Implementing Continue Statement with Basic Loop
Iterative Control-II DECLARE x NUMBER := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x)); x := x + 1; IF x < 3 THEN CONTINUE; END IF;
Iterative Control-II DBMS_OUTPUT.PUT_LINE ('Inside loop, after CONTINUE: x = ' || TO_CHAR(x)); EXIT WHEN x = 5; END LOOP; DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x)); END;
Iterative Control-II Continue When Statement Unconditionally Exit from current iteration of loop Statements after continue are skipped Control is transfer to next iteration Must be inside loop
Iterative Control-II Implementing Continue WHEN with Loop
Iterative Control-II DECLARE x NUMBER := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x)); x := x + 1; CONTINUE WHEN x < 3;
Iterative Control-II DBMS_OUTPUT.PUT_LINE ('Inside loop, after CONTINUE: x = ' || TO_CHAR(x)); EXIT WHEN x = 5; END LOOP; DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x)); END;
Iterative Control-II Implementing Continue WHEN with For Loop
Iterative Control-II declare val number(3):=3; BEGIN FOR i IN 1 .. 10 LOOP dbms_output.put_line('i=' || TO_CHAR(i)); CONTINUE WHEN (i+1) = val; dbms_output.put_line('Did not jump to the top of the loop'); END LOOP; END;
Iterative Control-II Nested Loops Loop within loop Any type of loop can be nested within any other or same type of loop Again one iteration of main loop complete inner loop is executed
Iterative Control-II Syntax for Nested Loops
Iterative Control-II LOOP – Main Loop Sequence of statements1 LOOP – Nested or inner loop Sequence of statements2 END LOOP; END
Iterative Control-II Implementing Nested Loops-I
Iterative Control-II BEGIN FOR v_outerloopcounter IN 1..2 LOOP FOR v_innerloopcounter IN 1..4 DBMS_OUTPUT.PUT_LINE('Outer Loop counter is ' || v_outerloopcounter || ' Inner Loop counter is ' || v_innerloopcounter); END LOOP; END LOOP; END;
Iterative Control-II Implementing Nested Loops-II
Iterative Control-II DECLARE v_counter1 INTEGER:=0; BEGIN WHILE v_counter1 < 3 LOOP DBMS_OUTPUT.PUT_LINE('v_counter1 : ' || v_counter1); LOOP DBMS_OUTPUT.PUT_LINE('v_counter2: ' || v_counter2);
Iterative Control-II v_counter2 := v_counter2 + 1; EXIT WHEN v_counter2 >= 2; END LOOP; v_counter1 := v_counter1+1; END;