Basic loop is the simplest form of loop construct in PL/SQL and hence also called as Simple loop. It encloses a sequence of statements between the keywords LOOP and END LOOP. It allows execution of its statements at least once. To keep the loop in finite state the EXIT statement is used.
The EXIT condition can be at the top of the loop or at the end of the loop as per convenience. Depending upon the circumstance we can make use of this loop as Pre-tested or Post-tested loop construct.
SET SERVEROUTPUT ON; DECLARE V_Num NUMBER := 1; BEGIN LOOP DBMS_OUTPUT.PUT_LINE('The Line '||V_Num||' Output is '||V_Num); V_Num :=V_Num+1; IF V_Num > 5 THEN EXIT; END IF; END LOOP; END; / The Line 1 Output is 1 The Line 2 Output is 2 The Line 3 Output is 3 The Line 4 Output is 4 The Line 5 Output is 5 The Total Lines of Output are 5 PL/SQL procedure successfully completed.
DECLARE V_Num NUMBER := 1; BEGIN LOOP IF V_Num > 5 THEN EXIT; END IF; DBMS_OUTPUT.PUT_LINE('The Line '||V_Num||' Output is '||V_Num); V_Num :=V_Num+1; END LOOP; DBMS_OUTPUT.PUT_LINE('The Total Lines of Output are '||(V_Num-1)); END; / The Line 1 Output is 1 The Line 2 Output is 2 The Line 3 Output is 3 The Line 4 Output is 4 The Line 5 Output is 5 The Total Lines of Output are 5 PL/SQL procedure successfully completed.
DECLARE V_Number NUMBER := 1; V_Output VARCHAR2(100); BEGIN LOOP EXIT WHEN V_Number >5; V_Output :=V_Output||' '||V_Number; V_Number :=V_Number+1; END LOOP; DBMS_OUTPUT.PUT_LINE('Output Line :'||V_Output); END; / Output Line : 1 2 3 4 5 PL/SQL procedure successfully completed.
DECLARE V_Number NUMBER :=1; V_Index NUMBER; V_Output VARCHAR2(50); BEGIN LOOP EXIT WHEN V_Number >5; V_Index :=1; LOOP EXIT WHEN V_Index >V_Number; V_Output :=V_Output||' '||V_Number; V_Index :=V_Index+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V_Output); V_Number :=V_Number+1; END LOOP; DBMS_OUTPUT.PUT_LINE('Output Line :'||V_Output); END; / 1 1 2 2 1 2 2 3 3 3 1 2 2 3 3 3 4 4 4 4 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Output Line : 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 PL/SQL procedure successfully completed.
DECLARE V_Number NUMBER :=1; V_Index NUMBER; V_Output VARCHAR2(50); BEGIN LOOP EXIT WHEN V_Number >5; V_Index :=1; V_Output :=NULL; LOOP EXIT WHEN V_Index >V_Number; V_Output :=V_Output||' '||V_Index; V_Index :=V_Index+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V_Output); V_Number :=V_Number+1; END LOOP; END; / 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 PL/SQL procedure successfully completed.
DECLARE V_Number NUMBER :=&GNumber; V_Reverse NUMBER :=0; V_Temp NUMBER; V_Div NUMBER; BEGIN V_Temp :=V_Number; LOOP EXIT WHEN V_Temp <=0; V_Div :=MOD(V_Temp, 10); V_Reverse :=V_Reverse*10+V_Div; V_Temp :=TRUNC(V_Temp/10); END LOOP; DBMS_OUTPUT.PUT_LINE('Reverse of '||V_Number||' is : '||V_Reverse); IF V_Number =V_Reverse THEN DBMS_OUTPUT.PUT_LINE('The number is a Palindrome'); ELSE DBMS_OUTPUT.PUT_LINE('The number isn''t a Palindrome'); END IF; END; / Enter value for gnumber: 12345 old 2: V_Number NUMBER :=&GNumber; new 2: V_Number NUMBER :=12345; Reverse of 12345 is : 54321 The number isn't a Palindrome PL/SQL procedure successfully completed. SQL> / Enter value for gnumber: 23432 old 2: V_Number NUMBER :=&GNumber; new 2: V_Number NUMBER :=23432; Reverse of 23432 is : 23432 The number is a Palindrome PL/SQL procedure successfully completed. SQL> / Enter value for gnumber: 0 old 2: V_Number NUMBER :=&GNumber; new 2: V_Number NUMBER :=0; Reverse of 0 is : 0 The number is a Palindrome PL/SQL procedure successfully completed. SQL> / Enter value for gnumber: -1221 old 2: V_Number NUMBER :=&GNumber; new 2: V_Number NUMBER :=-1221; Reverse of -1221 is : 0 The number isn't a Palindrome PL/SQL procedure successfully completed.
--Display all the Months between two given dates using BASIC Loop. DECLARE V_start_date DATE := '01-jan-2010'; --Signifies the Lower date. V_end_date DATE :='31-Aug-2010'; --Signifies the Upper date. BEGIN DBMS_OUTPUT.PUT_LINE('Months between '||V_start_date||' and '||V_end_date||' are :'); LOOP EXIT WHEN V_start_date > V_end_date; DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_start_date, 'Month')); V_Date1 :=ADD_MONTHS(V_start_date, 1); END LOOP; END; /
While writing programs using WHILE Loop, you should be very careful about the statement that control the exit from the loop. If the exit from loop condition is never met, the loop becomes infinite and program might go into a deadlock situation.