Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 The Oracle PL/SQL LOOP Construct      [Return To Index] Jump to:  
  Looking for the original pages? (formerly called "Morgan's Library") You can find them here.

Term: LOOP

Definition:
In Oracle PL/SQL, a LOOP is an iterative (repeating) control structure. Loops repeat a specified number of times based on one or more conditions. Inside the loop, one or more PL/SQL statements are evaluated and processed in the order they appear.

Loops are terminated automatically once they complete their iteration cycle, or if an EXIT condition is satisfied.

An EXIT condition may be produced by statements inside the loop that test one or more values. The statement logic and value tests may be used to produce an EXIT condition.

Example Syntax:
Oracle provides three types of loop constructs:

1. The Basic Loop
The BASIC loop repeats until a condition is met. Because the condition is tested at the end of the loop, the BASIC loop will always execute at least once.

LOOP
[Sequence of statements]
EXIT [condition]
END LOOP;


2. The FOR Loop
The FOR loop repeats, incrementing or decrementing its internal counter until the counter reaches its pre-programmed limit, set by the lower_bound and higher_bound parameters.

FOR counter IN [REVERSE] lower_bound..higher_bound LOOP
[Sequence of statements]
END LOOP;


3. The WHILE Loop
The WHILE loop repeats until a given condition is met. If the condition is not met it will repeat forever. If the condition is met or satisfied before the loop begins, it will not execute at all.

WHILE condition LOOP
statement_1;
statement_2;
. . .
statement_n;
END LOOP;



Example Usage:
The example FOR loop below will execute ten times, printing the word 'testing' each time.

BEGIN
FOR I IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE('testing');
END LOOP;
END;



Related Links:

Related Code Snippets:
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 228 users online    © 2009 psoug.org
PSOUG LOGIN
Username: 
Password: 
Forgot your password?