Plsql - LoopsThere may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages: PL/SQL provides the following types of loop to handle the looping requirements. Click the following links to check their detail.
Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements. With each iteration, the sequence of statements is executed and then control resumes at the top of the loop. Syntax: The syntax of a basic loop in PL/SQL programming language is:
LOOP
Sequence of statements; END LOOP; Here, sequence of statement(s) may be a single statement or a block of statements. An EXIT statement or an EXIT WHEN statement is required to break the loop. Example:
DECLARE
x number := 10; BEGIN LOOP dbms_output.put_line(x); x := x + 10; IF x > 50 THEN exit; END IF; END LOOP; -- after exit, control resumes here dbms_output.put_line('After Exit x is: ' || x); END; / When the above code is executed at SQL prompt, it produces the following result:
10
20 30 40 50 After Exit x is: 60 PL/SQL procedure successfully completed. You can use the EXIT WHEN statement instead of the EXIT statement:
DECLARE
x number := 10; BEGIN LOOP dbms_output.put_line(x); x := x + 10; exit WHEN x > 50; END LOOP; -- after exit, control resumes here dbms_output.put_line('After Exit x is: ' || x); END; / When the above code is executed at SQL prompt, it produces the following result:
10
20 30 40 50 After Exit x is: 60 PL/SQL procedure successfully completed. PL/SQL WHILE LOOP A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target statement as long as a given condition is true. Syntax:
WHILE condition LOOP
sequence_of_statements END LOOP; Example:
DECLARE
a number(2) := 10; BEGIN WHILE a < 20 LOOP dbms_output.put_line('value of a: ' || a); a := a + 1; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 PL/SQL procedure successfully completed. PL/SQL FOR LOOP A FOR LOOP is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax:
FOR counter IN initial_value .. final_value
LOOP sequence_of_statements; END LOOP; Here is the flow of control in a for loop:
DECLARE
a number(2); BEGIN FOR a in 10 .. 20 LOOP dbms_output.put_line('value of a: ' || a); END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 value of a: 20 PL/SQL procedure successfully completed. Reverse FOR LOOP Statement By default, iteration proceeds from the initial value to the final value, generally upward from the lower bound to the higher bound. You can reverse this order by using the REVERSE keyword. In such case, iteration proceeds the other way. After each iteration, the loop counter is decremented. However, you must write the range bounds in ascending (not descending) order. The following program illustrates this:
DECLARE
a number(2) ; BEGIN FOR a IN REVERSE 10 .. 20 LOOP dbms_output.put_line('value of a: ' || a); END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 20
value of a: 19 value of a: 18 value of a: 17 value of a: 16 value of a: 15 value of a: 14 value of a: 13 value of a: 12 value of a: 11 value of a: 10 PL/SQL procedure successfully completed. Nested loops in PL/SQL PL/SQL allows using one loop inside another loop. Following section shows few examples to illustrate the concept. The syntax for a nested basic LOOP statement in PL/SQL is as follows:
LOOP
Sequence of statements1 LOOP Sequence of statements2 END LOOP; END LOOP; The syntax for a nested FOR LOOP statement in PL/SQL is as follows:
FOR counter1 IN initial_value1 .. final_value1
LOOP sequence_of_statements1 FOR counter2 IN initial_value2 .. final_value2 LOOP sequence_of_statements2 END LOOP; END LOOP; The syntax for a nested WHILE LOOP statement in Pascal is as follows:
WHILE condition1 LOOP
sequence_of_statements1 WHILE condition2 LOOP sequence_of_statements2 END LOOP; END LOOP; Example: The following program uses a nested basic loop to find the prime numbers from 2 to 100:
DECLARE
i number(3); j number(3); BEGIN i := 2; LOOP j:= 2; LOOP exit WHEN ((mod(i, j) = 0) or (j = i)); j := j +1; END LOOP; IF (j = i ) THEN dbms_output.put_line(i || ' is prime'); END IF; i := i + 1; exit WHEN i = 50; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
2 is prime
3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime PL/SQL procedure successfully completed. Labeling a PL/SQL Loop PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and >>) and appear at the beginning of the LOOP statement. The label name can also appear at the end of the LOOP statement. You may use the label in the EXIT statement to exit from the loop. The following program illustrates the concept:
DECLARE
i number(1); j number(1); BEGIN << outer_loop >> FOR i IN 1..3 LOOP << inner_loop >> FOR j IN 1..3 LOOP dbms_output.put_line('i is: '|| i || ' and j is: ' || j); END loop inner_loop; END loop outer_loop; END; / When the above code is executed at SQL prompt, it produces the following result:
i is: 1 and j is: 1
i is: 1 and j is: 2 i is: 1 and j is: 3 i is: 2 and j is: 1 i is: 2 and j is: 2 i is: 2 and j is: 3 i is: 3 and j is: 1 i is: 3 and j is: 2 i is: 3 and j is: 3 PL/SQL procedure successfully completed. The Loop Control Statements Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. PL/SQL supports the following control statements. Labeling loops also helps in taking the control outside a loop. Click the following links to check their detail.
The EXIT statement in PL/SQL programming language has following two usages:
The syntax for an EXIT statement in PL/SQL is as follows:
EXIT;
Example:
DECLARE
a number(2) := 10; BEGIN -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; IF a > 15 THEN -- terminate the loop using the exit statement EXIT; END IF; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 PL/SQL procedure successfully completed. The EXIT WHEN Statement The EXIT-WHEN statement allows the condition in the WHEN clause to be evaluated. If the condition is true, the loop completes and control passes to the statement immediately after END LOOP. Following are two important aspects for the EXIT WHEN statement:
The syntax for an EXIT WHEN statement in PL/SQL is as follows:
EXIT WHEN condition;
The EXIT WHEN statement replaces a conditional statement like if-then used with the EXIT statement. Example:
DECLARE
a number(2) := 10; BEGIN -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; -- terminate the loop using the exit when statement EXIT WHEN a > 15; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 PL/SQL procedure successfully completed. CONTINUE statement The CONTINUE statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. In other words, it forces the next iteration of the loop to take place, skipping any code in between. Syntax: The syntax for a CONTINUE statement is as follows:
CONTINUE;
Example:
DECLARE
a number(2) := 10; BEGIN -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; IF a = 15 THEN -- skip the loop using the CONTINUE statement a := a + 1; CONTINUE; END IF; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19 PL/SQL procedure successfully completed. GOTO statement A GOTO statement in PL/SQL programming language provides an unconditional jump from the GOTO to a labeled statement in the same subprogram. NOTE: Use of GOTO statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a GOTO can be rewritten so that it doesn't need the GOTO. Syntax: The syntax for a GOTO statement in PL/SQL is as follows:
GOTO label;
.. .. << label >> statement; Example: DECLARE a number(2) := 10; BEGIN <<loopstart>> -- while loop execution WHILE a < 20 LOOP dbms_output.put_line ('value of a: ' || a); a := a + 1; IF a = 15 THEN a := a + 1; GOTO loopstart; END IF; END LOOP; END; / When the above code is executed at SQL prompt, it produces the following result:
value of a: 10
value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19 PL/SQL procedure successfully completed. Restrictions with GOTO Statement GOTO Statement in PL/SQL imposes the following restrictions:
|