ITERATIVE STATEMENTS:
**********************
LOOP:
*****
-->It is a process of executing 1 or more statements repeatedly until a condition is satisfied.
-->Loops repeat a statement or sequence of statements in multiple times.
-->It is used when we want to repeat the execution of one or more statements.
-->Conditions is a boolean value or expression.
Oracle supports to work with the following 3 Looping constructs
1.SIMPLE LOOP
2.WHILE LOOP
3.FOR LOOP
-- SINGLE LINE COMMENT
/* STATEMENTS */ --MULTILINE COMMENT
SIMPLE LOOP Syntax:
*******************
INFINITE LOOP:
**************
LOOP
STATEMENTS;
END LOOP;
FINITE LOOP:
************
LOOP
STATEMENTS;
EXIT WHEN CONDITION;
END LOOP;
EX:
***
DECLARE
N NUMBER:=1;
BEGIN
DBMS_OUTPUT.PUT_LINE(N);
END;
/
DECLARE
A NUMBER :=1;
B NUMBER :=2;
C NUMBER :=3;
D NUMBER :=4;
E NUMBER :=5;
BEGIN
DBMS_OUTPUT.PUT_LINE(A);
DBMS_OUTPUT.PUT_LINE(B);
DBMS_OUTPUT.PUT_LINE(C);
DBMS_OUTPUT.PUT_LINE(D);
DBMS_OUTPUT.PUT_LINE(E);
END;
/
--INFINITE LOOP
DECLARE
N NUMBER:=1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(N);
N:=N+1;
END LOOP;
END;
/
DECLARE
N NUMBER :=1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(N);
EXIT WHEN N=10;
N :=N+1;
END LOOP;
END;
/
--FINITE LOOP
DECLARE
N NUMBER:=1;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
LOOP
DBMS_OUTPUT.PUT_LINE(N);
EXIT WHEN N>=10;
N:=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
DECLARE
N NUMBER:=1;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
LOOP
DBMS_OUTPUT.PUT_LINE(N);
IF N>=10 THEN
EXIT;
END IF;
N:=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
--PRINT EACH CHARACTER OF A NAME ONE BY ONE UNTIL ALL CHARACTERS ARE PRINTED
DECLARE
V_NAME VARCHAR2(50):='ORACLE ERP';
V_LENGTH NUMBER;
V_CHAR_NAME VARCHAR2(50);
N NUMBER:=0;
BEGIN
V_LENGTH :=LENGTH(V_NAME);
LOOP
N :=N+1;
V_CHAR_NAME:=SUBSTR(V_NAME,N,1);
DBMS_OUTPUT.PUT_LINE('CHARACTER IS :'||V_CHAR_NAME);
EXIT WHEN N>=V_LENGTH;
END LOOP;
END;
/
WHILE LOOP SYNTAX:
******************
WHILE (CONDITION)
LOOP
STATEMENT1;
STATEMENT2;
STATEMENTN;
END LOOP;
/
-->Write a program to display 1st 5 natural numbers?
DECLARE
N NUMBER :=1;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
WHILE (N<=5)
LOOP
DBMS_OUTPUT.PUT_LINE(N);
N:=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
--Write a program to display 1st n natural numbers?
DECLARE
x NUMBER;
n NUMBER:=&n;
BEGIN
x:=1;
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
WHILE x<=n
LOOP
DBMS_OUTPUT.PUT_LINE(x);
x:=x+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
--To display 1 to 10 numbers horizontally
DECLARE
N NUMBER :=1;
V VARCHAR2(30);
BEGIN
WHILE N<=10
LOOP
V :=V||' '||N;
N :=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(V);
END;
/
-->Write a program to display 1st n ODD numbers?
DECLARE
x NUMBER;
n NUMBER:=&n;
BEGIN
x:=1;
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
WHILE x<=n
LOOP
DBMS_OUTPUT.PUT_LINE(x);
x:=x+2;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
-->Write a program to display Mathematical Table Of a Given Number?
DECLARE
n NUMBER:=&n;
i NUMBER;
p NUMBER;
BEGIN
i:=1;
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
WHILE i<=10
LOOP
p:=n*i;
DBMS_OUTPUT.PUT_LINE(n||' * '||i||' = '||p);
i:=i+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
--PRINT EACH CHARACTER OF A NAME ONE BY ONE UNTIL ALL CHARACTERS ARE PRINTED
DECLARE
V_NAME VARCHAR2(50):='ORACLE ERP';
V_LENGTH NUMBER;
V_CHAR_NAME VARCHAR2(50);
N NUMBER:=0;
BEGIN
V_LENGTH :=LENGTH(V_NAME);
WHILE (N<V_LENGTH)
LOOP
N :=N+1;
V_CHAR_NAME:=SUBSTR(V_NAME,N,1);
DBMS_OUTPUT.PUT_LINE('CHARACTER IS :'||V_CHAR_NAME);
END LOOP;
END;
FOR LOOP SYNTAX:
****************
-->This looping construct will support to execute 1 or more statements based on given range of values.
-->Variable used at FOR looping construct does not required its declaration.
-->Variable need not be initialised and it does not require increment.
-->Variable will act like a local variable, which means it works only in that block where it is used.
SYNTAX:
*******
FOR VARIABLENAME IN LOWERBOUND..UPPERBOUND
LOOP
STATEMENTS;
END LOOP;
/
FOR var IN [ REVERSE ] lower_bound..upper_bound
LOOP
statement 1;
statement 2;
.......
statement n;
END LOOP;
When REVERSE option is used it will make a loop work in reverse order.
/
EX:
****
-->Write a program to display 1st 5 natural numbers using FOR looping construct?
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
FOR I IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('NUMBER :'||I);
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
DECLARE
A NUMBER :=1;
B NUMBER :=10;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE FOR LOOP');
FOR I IN A..B
LOOP
DBMS_OUTPUT.PUT_LINE('Inside For Loop :'||I);
END LOOP;
DBMS_OUTPUT.PUT_LINE('AFTER FOR LOOP');
END;
/
-->Write a program to display 1st n natural numbers using FOR looping construct?
DECLARE
n NUMBER:=&n;
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
FOR i IN 1..n
LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
-->Write a program to display 1st 5 Natural Numbers in Reverse Order?
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
FOR i IN REVERSE 1..5
LOOP
DBMS_OUTPUT.PUT_LINE('Inside For Loop:'||I);
END LOOP;
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
-->Write a program to display Reverse of a given String?
DECLARE
str VARCHAR2(12):='&str';
rev VARCHAR2(12);
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE LOOP');
rev:=' ';
FOR i IN REVERSE 1..LENGTH(str)
LOOP
rev:=rev||SUBSTR(str,i,1);
END LOOP;
DBMS_OUTPUT.PUT_LINE('REVERSE OF A STRING IS ......'||rev);
DBMS_OUTPUT.PUT_LINE('OUT OF THE LOOP');
END;
/
--PRINT EACH CHARACTER OF A NAME ONE BY ONE UNTIL ALL CHARACTERS ARE PRINTED
DECLARE
V_NAME VARCHAR2(50):='ORACLE ERP';
V_LENGTH NUMBER;
V_CHAR_NAME VARCHAR2(50);
--N NUMBER:=0;
BEGIN
V_LENGTH :=LENGTH(V_NAME);
--WHILE(N<V_LENGTH)
FOR N IN 1..V_LENGTH
LOOP
--N :=N+1;
V_CHAR_NAME:=SUBSTR(V_NAME,N,1);
DBMS_OUTPUT.PUT_LINE('CHARACTER IS :'||V_CHAR_NAME);
END LOOP;
END;
/
--multiplication table using for loop
DECLARE
A NUMBER :=:A;
B NUMBER;
BEGIN
FOR I IN 1..10
LOOP
B :=A * I;
DBMS_OUTPUT.PUT_LINE(A||' * '||I||' = '||B);
END LOOP;
END;
/
*
**
***
****
*****
/
DECLARE
RES VARCHAR2(20);
BEGIN
FOR I IN 1..5
LOOP
RES :='';
FOR J IN 1..I
LOOP
RES :=RES||'*'||' ';
END LOOP;
DBMS_OUTPUT.PUT_LINE(RES);
END LOOP;
END;
/
*****
****
***
**
*
/
DECLARE
RES VARCHAR2(20);
BEGIN
FOR I IN REVERSE 1..5
LOOP
RES :='';
FOR J IN 1..I
LOOP
RES :=RES||'*'||' ';
END LOOP;
DBMS_OUTPUT.PUT_LINE(RES);
END LOOP;
END;
/
No comments:
Post a Comment