Unit4 Iterative Statements
Unit4 Iterative Statements
4 UNIT-4-Iterative Statements
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;
end loop;
end;
/
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 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;
/
Output:
- 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.
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.
Output: