WHILE LOOP is also used for the itteration of block of statement.
The best usable of while is when the number of itteration are unknown.
Syntax:
begin
while condition loop
statement1;
statement2;
statement3;
.
.
statementn;
end loop;
In the above example, while and loop are the keywords. and condition is the critarea , which decides the number of itteration.
Example:
declare
result number:=0;
counter number:=1;
table_num number:= &Enter_number_For_table;
begin
while counter <=10 loop
result:=counter*table_num;
dbms_output.put_line(table_num||' * '||counter||' = '||result);
counter:=counter+1;
end loop;
dbms_output.put_line('M Talha Zubair, Out from loop');
end;
Result:
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
M Talha Zubair, Out from loop
While Loop with Boolean Variable as test Variable:
Example:
declare
v_test boolean:=true;
counter number:=1;
begin
while v_test loop
dbms_output.put_line(counter);
counter:=counter+1;
if counter=10 then
exit;
end if;
end loop;
dbms_output.put_line('M Talha Zubair, Out from loop');
end;
Result:
1
2
3
4
5
6
7
8
9
M Talha Zubair, Out from loop
No comments:
Post a Comment