Simple Loops
Simple Loops
Start loop
Loop
Stmt1;
Stmt2;
Stmt N; Execute stmts
End loop;
End loop
Exit Start loop
No
Execute stmt
Loop
Stmt1; Is exit condn
true
Stmt2; yes
If condition then Exit loop
Exit;
End if; Next stmt
End loop;
Stmt3;
Declare
V_counter number:=0;
Begin
Loop
Dbms_output.put_line(‘v_counter = ’ ||
v_counter);
Exit;
End loop;
End;
WHILE LOOP
WHILE cond loop
stmt1; No
Is condn true
stmt2;
………….. Yes
Next stmt
While Loop
Declare
v_counter number:=5;
Begin
While v_counter<5 loop
Dbms_output.put_line('v_counter:' || v_counter);
v_counter:=v_counter-1;
End loop;
dbms_output.put_line('done');
End;
Numeric For Loop
For loop_counter IN [REVERSE] lower_limit…upper_limit loop
stmt1;
Initialize counter
stmt2;
…………
StmtN; Is counter
bet lower &
end loop; Upper
limits
yes
No
Exeute stmt
Increment counter
Next stmt
Begin
For v_counter IN 1..5 loop
Dbms_output.put_line(‘v_counter’ || v_counter);
End loop;
End;
v_counter 1
v_counter 2
v_counter 3
v_counter 4
v_counter 5
Statement processed.
Functions
1.Functions are another type of stored code and very similar to
procedures.
2.Functions must have return clause .
Function Syntax
SQL> /
Function created.
DECLARE
result number;
BEGIN
result:=evenodd(&num);
IF result=1 then
DBMS_OUTPUT.PUT_LINE('THE NUMBER IS EVEN!');
ELSE
DBMS_OUTPUT.PUT_LINE('THE NUMBER IS ODD!');
END IF;
DBMS_OUTPUT.PUT_LINE('DONE!');
END;
CREATE A FUNCTION TO FIND THE FACTORIAL OF A NUMBER.
CREATE A FUNCTION TO FIND THE SUM OF THE 1ST 100 ODD NUMBERS.
CREATE A FUNCTION TO FIND THE SUM OF THE 1ST 100 EVEN NUMBERS