---
description: In this tutorial, you learn For Loop statement and Nested Loops with Syntax and Examples.
title: Oracle PL/SQL FOR LOOP with Example
image: https://www.guru99.com/images/oracle-plsql-for-loop-1.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Oracle PL/SQL FOR Loop runs a block of code a known number of times between a lower and higher limit. The loop variable is declared implicitly and self-increments, so no manual counter is needed, and the REVERSE keyword lets the loop count downward.

* 🔢 **Best For:** A FOR loop suits a known iteration count rather than an open-ended condition.
* 📏 **Range:** A lower and higher limit define how many times the loop runs.
* ⚙️ **Implicit Counter:** The loop variable is declared automatically and increments on its own.
* 🎯 **Scope:** The loop variable exists only inside the loop.
* 🔄 **REVERSE:** Adding REVERSE before the lower limit makes the loop count down.
* 🪜 **Nested:** Loops can be nested, and the inner loop runs fully for each outer iteration.
* 🚪 **Auto Exit:** The loop ends automatically when the variable leaves the range.

[ Read More ](javascript:void%280%29;) 

![Oracle PL/SQL For Loop](https://www.guru99.com/images/oracle-plsql-for-loop-1.png)

## What is a FOR Loop?

The “FOR LOOP” statement is best suited when you want to execute code for a known number of times, rather than based on some other condition.

In this loop, a lower limit and a higher limit are specified, and as long as the loop variable is between this range, the loop is executed.

The loop variable is self-incremental, so no explicit increment operation is needed. The loop variable need not be declared, as it is declared implicitly.

FOR <loop_variable> in <lower_limit> .. <higher_limit>
LOOP
<execution block starts>
.
.
.
<execution_block_ends>
END LOOP;

**Syntax Explanation:**

* The keyword ‘FOR’ marks the beginning of the loop and ‘END LOOP’ marks the end.
* The loop variable is evaluated every time before executing the execution part.
* The execution block contains all the code that needs to run and can contain any executable statement.
* The loop\_variable is declared implicitly during the execution of the loop, and its scope is only inside the loop.
* When the loop variable moves out of range, control exits the loop.
* The loop can be made to work in reverse order by adding the keyword ‘REVERSE’ before the lower\_limit.

**Example 1:** In this example, we print numbers from 1 to 5 using a FOR loop statement.

[![FOR loop printing numbers 1 to 5](https://www.guru99.com/images/PL-SQL/110215_0850_LoopsConcep7.png)](https://www.guru99.com/images/PL-SQL/110215%5F0850%5FLoopsConcep7.png)

BEGIN
dbms_output.put_line('Program started.');
FOR a IN 1 .. 5
LOOP
dbms_output.put_line(a);
END LOOP;
dbms_output.put_line('Program completed.');
END;
/

**Code Explanation:**

* **Code line 2:** Printing the statement “Program started”.
* **Code line 3:** The keyword ‘FOR’ marks the beginning of the loop and the loop\_variable ‘a’ is declared. It now takes values from 1 to 5.
* **Code line 5:** Prints the value of ‘a’.
* **Code line 6:** The keyword ‘END LOOP’ marks the end of the execution block.
* Line 5 continues to execute until ‘a’ reaches the value 6, at which point the condition fails and control exits the loop.
* **Code line 7:** Printing the statement “Program completed”.

## Nested Loops

Loop statements can also be nested. The outer and inner loops can be of different types. In a nested loop, for every iteration of the outer loop, the inner loop executes fully.

[![Nested loop structure](https://www.guru99.com/images/PL-SQL/110215_0850_LoopsConcep8.png)](https://www.guru99.com/images/PL-SQL/110215%5F0850%5FLoopsConcep8.png)

LOOP --outer
<execution block starts>
LOOP --inner
<execution_part>
END LOOP;
<execution_block_ends>
END LOOP;

**Syntax Explanation:**

* The outer loop has one more loop inside it.
* The loops can be of any type, and the execution functionality is the same.

### RELATED ARTICLES

* [Oracle PL/SQL Data Types: Boolean, Number, Date \[Example\] ](https://www.guru99.com/pl-sql-data-types.html "Oracle PL/SQL Data Types: Boolean, Number, Date [Example]")
* [PL/SQL Variable Scope & Inner Outer Block: Nested Structure ](https://www.guru99.com/nested-blocks-pl-sql.html "PL/SQL Variable Scope & Inner Outer Block: Nested Structure")
* [Oracle PL/SQL BULK COLLECT: FORALL Example ](https://www.guru99.com/pl-sql-bulk-collect.html "Oracle PL/SQL BULK COLLECT: FORALL Example")
* [Autonomous Transaction in Oracle PL/SQL ](https://www.guru99.com/pl-sql-tcl-statements.html "Autonomous Transaction in Oracle PL/SQL")

**Example 1:** In this example, we print numbers from 1 to 3 using a FOR loop statement. Each number is printed as many times as its value.

[![Nested FOR loop example part 1](https://www.guru99.com/images/PL-SQL/110215_0850_LoopsConcep9.png)](https://www.guru99.com/images/PL-SQL/110215%5F0850%5FLoopsConcep9.png)

[![Nested FOR loop example part 2](https://www.guru99.com/images/PL-SQL/110215_0850_LoopsConcep10.png)](https://www.guru99.com/images/PL-SQL/110215%5F0850%5FLoopsConcep10.png)

DECLARE
b NUMBER;
BEGIN
dbms_output.put_line('Program started');
FOR a IN 1..3
LOOP
b:= 1;
WHILE (a>=b)
LOOP
dbms_output.put_line(a);
b:= b+1;
END LOOP;
END LOOP;
dbms_output.put_line('Program completed');
END;
/

**Code Explanation:**

* **Code line 2:** Declaring the variable ‘b’ as ‘NUMBER’ data type.
* **Code line 4:** Printing the statement “Program started”.
* **Code line 5:** The keyword ‘FOR’ marks the beginning of the loop and the loop\_variable ‘a’ is declared. It now takes values from 1 to 3.
* **Code line 7:** Resetting the value of ‘b’ to ‘1’ each time.
* **Code line 8:** The inner [while loop](https://www.guru99.com/oracle-plsql-while-loop.html) checks for the condition a>=b.
* **Code line 10:** Prints the value of ‘a’ as long as the above condition is satisfied.
* **Code line 14:** Printing the statement “Program completed”.

## FOR Loop and REVERSE FOR Loop

By default a FOR loop counts upward from the lower limit to the higher limit. Adding the REVERSE keyword makes it count downward while keeping the limits in the same order. Both forms are shown together below so the difference is clear.

| Form       | Header                  | Order of values |
| ---------- | ----------------------- | --------------- |
| Ascending  | FOR a IN 1 .. 5         | 1, 2, 3, 4, 5   |
| Descending | FOR a IN REVERSE 1 .. 5 | 5, 4, 3, 2, 1   |

Note that even with REVERSE, the lower limit is still written before the higher limit; only the direction of iteration changes. For loops whose count is not known in advance, the [basic loop](https://www.guru99.com/loops-pl-sql.html) is the better choice.

## FAQs

⚙️ Does the FOR loop variable need to be declared?

No. The FOR loop declares its variable implicitly, and it exists only inside the loop. Declaring a variable of the same name outside does not affect the one used by the loop.

🔄 What does the REVERSE keyword do?

REVERSE makes the loop iterate from the higher limit down to the lower limit. The limits are still written low to high; only the order in which the values are visited is reversed.

🔢 When should a FOR loop be used instead of a WHILE loop?

Use a FOR loop when the number of iterations is known in advance, such as a fixed range. Use a WHILE loop when the loop should continue while a condition holds and the count is not fixed.

🤖 Can AI convert a basic loop into a FOR loop?

Yes, when the basic loop simply counts between two fixed values. AI can replace the manual counter and EXIT with a FOR range, making the code shorter. Confirm the boundaries match the original.

🎯 Can the FOR loop variable be changed inside the loop?

No. The loop variable is read-only inside the loop, so an assignment to it raises a compile error. To alter the flow, use a separate variable or exit the loop with EXIT.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter](https://www.guru99.com/images/footer-email-avatar-imges-1.png) Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/oracle-plsql-for-loop-1.png","url":"https://www.guru99.com/images/oracle-plsql-for-loop-1.png","width":"700","height":"250","caption":"Oracle PL/SQL FOR LOOP","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/oracle-plsql-for-loop.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/pl-sql","name":"PL-SQL"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/oracle-plsql-for-loop.html","name":"Oracle PL/SQL FOR LOOP with Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/oracle-plsql-for-loop.html#webpage","url":"https://www.guru99.com/oracle-plsql-for-loop.html","name":"Oracle PL/SQL FOR LOOP with Example","dateModified":"2026-07-22T16:41:43+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/oracle-plsql-for-loop-1.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/oracle-plsql-for-loop.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown","description":"I'm Fiona brown, a Full Stack Developer with over a decade of experience, sharing practical guides on robust and scalable application development.","url":"https://www.guru99.com/author/fiona","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/fiona-brown-author.png","url":"https://www.guru99.com/images/fiona-brown-author.png","caption":"Fiona Brown","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"PL-SQL","headline":"Oracle PL/SQL FOR LOOP with Example","description":"In this tutorial, you learn For Loop statement and Nested Loops with Syntax and Examples.","keywords":"pl-sql","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown"},"dateModified":"2026-07-22T16:41:43+05:30","image":{"@id":"https://www.guru99.com/images/oracle-plsql-for-loop-1.png"},"copyrightYear":"2026","name":"Oracle PL/SQL FOR LOOP with Example","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Does the FOR loop variable need to be declared?","acceptedAnswer":{"@type":"Answer","text":"No. The FOR loop declares its variable implicitly, and it exists only inside the loop. Declaring a variable of the same name outside does not affect the one used by the loop."}},{"@type":"Question","name":"What does the REVERSE keyword do?","acceptedAnswer":{"@type":"Answer","text":"REVERSE makes the loop iterate from the higher limit down to the lower limit. The limits are still written low to high; only the order in which the values are visited is reversed."}},{"@type":"Question","name":"When should a FOR loop be used instead of a WHILE loop?","acceptedAnswer":{"@type":"Answer","text":"Use a FOR loop when the number of iterations is known in advance, such as a fixed range. Use a WHILE loop when the loop should continue while a condition holds and the count is not fixed."}},{"@type":"Question","name":"Can AI convert a basic loop into a FOR loop?","acceptedAnswer":{"@type":"Answer","text":"Yes, when the basic loop simply counts between two fixed values. AI can replace the manual counter and EXIT with a FOR range, making the code shorter. Confirm the boundaries match the original."}},{"@type":"Question","name":"Can the FOR loop variable be changed inside the loop?","acceptedAnswer":{"@type":"Answer","text":"No. The loop variable is read-only inside the loop, so an assignment to it raises a compile error. To alter the flow, use a separate variable or exit the loop with EXIT."}}]}],"@id":"https://www.guru99.com/oracle-plsql-for-loop.html#schema-1148947","isPartOf":{"@id":"https://www.guru99.com/oracle-plsql-for-loop.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/oracle-plsql-for-loop.html#webpage"}}]}
```
