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

Unit4 Iterative Statements

The document provides an overview of iterative statements in PL/SQL, including Loop..End Loop, For Loop, and While Loop, detailing their syntax and usage. It explains control statements like EXIT and CONTINUE, which modify the flow of loops, and briefly mentions the Goto statement, although it is not part of the syllabus. Examples are provided to illustrate how to implement these statements in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit4 Iterative Statements

The document provides an overview of iterative statements in PL/SQL, including Loop..End Loop, For Loop, and While Loop, detailing their syntax and usage. It explains control statements like EXIT and CONTINUE, which modify the flow of loops, and briefly mentions the Goto statement, although it is not part of the syllabus. Examples are provided to illustrate how to implement these statements in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

FYBCA-205-INTRODUCTION TO RDBMS

4 UNIT-4-Iterative Statements

4.1 Iterative statements


- Iterative statements allow to execute sequential statement(s) multiple times.
- It includes looping statements where same code is repeated till the condition
becomes true.
- PL/SQL provides following looping statements:

4.1.1 Loop..End Loop


- The basic loop statement is written between LOOP and END LOOP.
- It repeats the statement(s) multiple times.
- To terminate this loop, EXIT or EXIT WHEN statement is required.

Syntax:
LOOP
statements;
EXIT; (or EXIT WHEN condition;)
END LOOP;

- First of all, numbers of statements are executed and when the condition related to
EXIT or EXIT WHEN is executed then the loop is break and move to the statement
written after END LOOP.
- There is a need to initialize variable before LOOP and it must be increment or
decrement externally within LOOP.
Example: Display first four natural numbers using EXIT
declare
i int:=1;
begin
loop
dbms_output.put_line('The value of i is ' ||i);
i:=i+1;
if i=5 then
exit;
end if;

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 1


FYBCA-205-INTRODUCTION TO RDBMS

end loop;
end;
/

Example: Display first 4 natural numbers using EXIT WHEN


declare
i int:=1;
begin
loop
dbms_output.put_line('The value of i is ' ||i);
i:=i+1;
exit when i=5;
end loop;
end;
/
Output:

4.1.2 For.. Loop


- For loop is used when you want to repeat the statements fixed numbers of time.
Syntax:
FOR counter IN [REVERSE] first_val .. last_val
LOOP
statements;
END LOOP;

- PL/SQL initializes “counter=first_val” once and compares it with last_val. If condition


becomes true then statements are executed and counter is increment or decrement
by one.
- To execute from final_val to initial_val (means in descending order) then REVERSE
keyword is used.
- Nested for loop is also allowed.

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 2


FYBCA-205-INTRODUCTION TO RDBMS

Example: Display the sum of first 5 natural numbers

declare
-- i int; No need to initialize
s int:=0;
begin
for i in 1 .. 5 loop
s:=s+i;
end loop;
dbms_output.put_line('Sum is '||s);
end;
/

Output:
Sum is 15

Example: Display first 5 natural numbers in descending order.


declare
i int;
begin
for i in reverse 1 .. 5 loop
dbms_output.put_line('Value of i is '||i);
end loop;
end;
/
Output:

Example: Display the students’ rollno, name and marks whose marks is less than 40
begin
dbms_output.put_line('Rollno Name Marks');
for i in (select * from stud_info where marks<40)
loop
dbms_output.put_line(i.rno||' ' ||i.name ||' '||i.marks);
end loop;

end;
/

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 3


FYBCA-205-INTRODUCTION TO RDBMS

Output:

4.1.3 While Loop


- While loop is used when you want to repeat the statements as per specific condition.
Syntax:
WHILE condition LOOP
statements;
END LOOP;

- Condition is checked and the numbers of statements are repeated till the condition
becomes false.
- There is a need to initialize variable before LOOP and it must be increment or
decrement externally within LOOP.

Example: Display the numbers between 10 to 15


declare
i int:=10;
begin
while i<=15 loop
dbms_output.put_line('Value of i is '||i);
i:=i+1;
end loop;
end;
/
Output:

4.2 Control statements


- Control statements are used within looping statements.
- It changes the execution from its normal sequence.

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 4


FYBCA-205-INTRODUCTION TO RDBMS

4.2.1 EXIT Loop


- It is used to break the loop and exit from loop.
- It moves control at the next statement after END LOOP.
- Syntax:
1. EXIT;
2. EXIT WHEN condition;
- EXIT statement:
o It is used when you want to exit from loop without specifying any condition.
o To specify the condition, IF statement is used.
- EXIT WHEN condition;
o It is used when you want to exit from loop as per specific condition.
o When the specified condition becomes true then the control transfer to the
next statement after END LOOP.

4.2.2 Continue (New feature added in Oracle 11g-Can’t work in older versions)
- It is used when you want to return back to the starting of next iteration of loop.
- It ends the current iteration and passes the control to the first line of loop.
- Normally it is written within IF statement.

Example: Display even numbers between 1 to 10.


declare
i int;
begin
for i in 1..10 loop
if mod(i,2)<>0 then
CONTINUE;
end if;
dbms_output.put_line('Value of i is '||i);
end loop;
end;
/

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 5


FYBCA-205-INTRODUCTION TO RDBMS

4.3 Goto statement: (Not in syllabus)


- Goto is a jumping statement which allows pointer to move from one location to
another within PL/SQL block.
- Syntax:
GOTO label;
….
<<label>>
Statements;
Example: Display even numbers between 1 to 10
declare
i int;
begin
for i in 1..10 loop
if mod(i,2)<>0 then
goto a;
end if;
dbms_output.put_line('Value of i is '||i);
<<a>>
NULL;
end loop;
end;
/

Output:

DR. HIRAL A. PATEL, MS. NAYNA MISTRY, MS. MANSI SHAH 6

You might also like