본문 바로가기

개발

[DB] Oracle(오라클)/PLSQL: FOR Loop(for loop 반복문)


The syntax for the FOR Loop is:

FOR loop_counter IN [REVERSE] lowest_number..highest_number
LOOP
     {.statements.}
END LOOP;

You would use a FOR Loop when you want to execute the loop body a fixed number of times.


Let's take a look at an example.

FOR Lcntr IN 1..20
LOOP
     LCalc := Lcntr * 31;
END LOOP;

This example will loop 20 times. The counter will start at 1 and end at 20.


The FOR Loop can also loop in reverse. For example:

FOR Lcntr IN REVERSE 1..15
LOOP
     LCalc := Lcntr * 31;
END LOOP;

This example will loop 15 times. The counter will start at 15 and end at 1. (loops backwards)