PLSQL - While Loop


WHILE
:

We can use While loop to evaluate before each iteration

Statement executed and control resumes at the top of the loop when condition evaluates to TRUE

Loop is bypassed and control passes to next statement when condition evaluates to FALSE

Number of iterations depends on the condition and is unknown until the loop completes


WHILE <condition>

LOOP

statements;

END LOOP;

Example to display 1 to 15 using while loop

declare

x number;

Begin

x := 1;

while x <=15

Loop

dbms_output.put_line(x);

x := x + 1;

End Loop;

end;


Example to Force a loop to complete unconditionally


declare

x number;

/*Use exit after z reaches to 8*/

Begin

x := 1;

while x <=15

Loop

dbms_output.put_line(x);

x := x + 1;

exit when x = 8;

End Loop;

end;

Complete Tutorial

*/

No comments:

Post a Comment