0% found this document useful (0 votes)
2 views

Simple Loops

The document provides an overview of various loop constructs in programming, including simple loops, while loops, and numeric for loops, along with their syntax and examples. It also discusses functions, highlighting their similarities to procedures, the requirement for a return clause, and provides syntax for creating functions. Additionally, it mentions specific functions that can be created, such as checking for even or odd numbers, calculating factorials, and summing odd or even numbers.

Uploaded by

seemahpatil2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Simple Loops

The document provides an overview of various loop constructs in programming, including simple loops, while loops, and numeric for loops, along with their syntax and examples. It also discusses functions, highlighting their similarities to procedures, the requirement for a return clause, and provides syntax for creating functions. Additionally, it mentions specific functions that can be created, such as checking for even or odd numbers, calculating factorials, and summing odd or even numbers.

Uploaded by

seemahpatil2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

stmtN Execute stmt


No
End loop
End loop

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

create [or replace] function function_name


(parameter list)
return datatype;
IS
BEGIN
<body>
RETURN (return_value);
END;
Functions
CREATE OR REPLACE FUNCTION evenodd(num number)
return number
IS
result number;
BEGIN
IF mod(num,2)=0 then
result:=1;
ELSE
result:=2;
END IF;
return result;
end;

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 CHECK IF A NUMBER IS PRIME OR NOT.

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

You might also like