Oracle PL/SQL allows for the creation of stored procedures, functions, and triggers. One of the basic control structures in PL/SQL is the LOOP statement, which allows for repeated execution of a block of code.
There are several different types of loops in PL/SQL, including:
Simple LOOP repeatedly executes a block of code until a specific condition is met. The basic syntax for a simple loop is:
LOOP [Code to be executed] [Exit condition] END LOOP;
WHILE LOOP repeatedly executes a block of code as long as a specific condition is true. The basic syntax for a while loop is:
WHILE [condition] LOOP [Code to be executed] [Exit condition] END LOOP;
FOR LOOP repeatedly executes a block of code a specified number of times. The basic syntax for a for loop is:
FOR [counter] IN [range] LOOP [Code to be executed] [Exit condition] END LOOP;
Each type of loop can be used to perform a specific task. It is important to note that it is necessary to include an exit condition in order to prevent infinite looping.
LOOP example
DECLARE v_num NUMBER:=0; BEGIN LOOP v_num:=v_num+1; DBMS_OUTPUT.put_line(v_num); IF v_num = 3 THEN EXIT; END IF; END LOOP; END;
Output:
1
2
3