recent

Titulo

The Loop

According to Computer Hope- an online magazine, a LOOP is described as the process of a software program or a script that repeats the same instruction or processes the same information over and over until the computer receives the order to stop. If the loop is not handled correctly can cause the computer to become slower as it overwhelms the processor causing it to get stuck in an infinite loop. You will find loop in any programming languages including PL/SQL. Below, we will demo all the loop examples that are available on PL/SQL.

Loop... End Loop:

SET serveroutput ON;
BEGIN 
  LOOP 
    dbms_output.Put_line (lv_cnt_num); 
    lv_cnt_num := lv_cnt_num + 1; 
    EXIT 
  WHEN lv_cnt_num >5;
  END LOOP; 
END;
/ 

PL/SQL procedure successfully completed.
1
2
3
4
5

WHILE... LOOP: 

Contains a check at the top of the loop in the LOOP clause itself. For each iteration of the loop thw condition is checked and if this is TRUE, the loop continues or if the condition is False the looping action stops.

DECLARE 
  lv_cnt_num NUMBER(2) := 1; 
BEGIN 
  WHILE lv_cnt_num <= 5 
  LOOP 
    dbms_output.put_line(lv_cnt_num); 
    lv_cnt_num := lv_cnt_num + 1; 
  END LOOP; 
END;
/

PL/SQL procedure successfully completed.
1
2
3
4
5

FOR LOOP: 

This loop completes the same job of iterating; however, this type of loop indicates how many times to loop by providing a range at the beginning of the statement.

BEGIN 
    FOR i IN 1..5 LOOP 
        dbms_output.Put_line(i); 
    END LOOP; 
END; 
/ 

PL/SQL procedure successfully completed.
1
2
3
4
5

 Reverse LOOP:

 This is similar to that of for loop but reverse the order of loop that it executes.

BEGIN 
    FOR i IN REVERSE 1..5 LOOP 
        dbms_output.Put_line(i); 
    END LOOP; 
END; 
/ 

Output PL/SQL procedure successfully completed.
5
4
3
2
1


Interested in working with me? I can be reached at pbaniya04[at]gmail.com for any questions, consulting opportunities or you may drop a line to say HELLO. Thank your again for visiting my blog and looking forward to serving you more.

Have a Database-ious Day!

No comments

Powered by Blogger.