oracle

Tuesday, 2 May 2017

PL/SQL: SIMPLE LOOPS

LOOPS executes statement or set of statement  or part of program for several times.
First of all we discuss Simple loop.

Simple Loop:
Syntax:
Loop
statement 1;
statement 2;
.
.
.
statement n;
End loop;

Example:
declare
counter number:=0;
result number;
num number:=&Enter_Number_For_Table;
begin
loop
counter := counter+1;
result := num*counter;
dbms_output.put_line(num||' * '||counter||' = '||result);
if counter>=10 then
exit;
end if;
end loop;

end;

Result:
Enter_Number_For_table = 14

14 * 1 = 14
14 * 2 = 28
14 * 3 = 42
14 * 4 = 56
14 * 5 = 70
14 * 6 = 84
14 * 7 = 98
14 * 8 = 112
14 * 9 = 126
14 * 10 = 140

First we declare variables for counter , result and a number for which table is created.
After begin part, We Define a loop. Increment by 1 in counter. and give way  how generate or calcute the result.
Then we print result for every execution.
Then we use IF STATEMENT  to stop or finalize the loop. Then end if and end of loop and end of program.
we can also Finalize the Loop as
Exit when counter>=10;
 The result of all query is above mentioned. If any problem,then feel free to ask.

No comments:

Post a Comment