You are on page 1of 5

Chapter 03:Control structures and loops

________________________________________
1) if statement :
_________________
if condition then
_______________________s1;
_______________________s2;
_______________________s3;
end if;
------------------------------------------------------------------------if condition then
__________________s1
__________________s2
__________________s3
else
__________________s4
__________________s5
__________________s6
end if;
--------------------------------------------------------------------------if Condition1 then
__________________
__________________
__________________
[elsif condition2 then ]
_________________
__________________
_________________
[else]
__________________
__________________
__________________
end if;
---------------------------------------------------------------------------Example 1
_________
declare
v_sal number (9) ;
begin
select sal into v_sal from emp where empno = 7788 ;
if v_sal between 3000 and 4000 then
dbms_output.put_line('salary is between 3000 and 4000') ;
update emp set sal = sal * 1.2 where empno = 7788;
end if;
end;

_____________________________________________________________________________
Example 2
_________
declare
v_sal number (9) ;
begin
select sal into v_sal from emp where empno = 7788 ;
if v_sal between 2000 and 3000 then
dbms_output.put_line('salary is between 2000 and 3000') ;
update emp set sal = sal * 1.2 where empno =7788;
else
dbms_output.put_line(' salary out of range 2000 and 3000');
end if;
end;
_____________________________________________________________________________
Example 3
_________
declare
v_sal number (9) ;
begin
select sal
into v_sal
from emp
where empno = 7788;
if v_sal between 2000 and 3000 then
dbms_output.put_line('salary is between 2000 and 3000') ;
update emp set sal = sal * 1.2 where empno =7788;
elsif v_sal between 3001 and 4000 then
dbms_output.put_line('salary is between 3001 and 4000') ;
update emp set sal = sal * 1.3 where empno =7788;
else
dbms_output.put_line(' salary out of range 3001and 4000');
end if;
end;
_____________________________________________________________________________
2) Loops :
__________
1) Basic Loop
2) While loop
3) For Loop
2.1) Basic Loop
_______________
syntax
______
loop
____________s1
____________s2

____________s3
exit when condition ;
end loop;

Example
_______
Declare
v_counter

number(3) := 0;

begin
loop
v_counter := v_counter + 1 ;
dbms_output.put_line(v_counter);
exit when v_counter=10;
end loop;
end;
_____________________________________________________________________________
2.2) While loop
________________
while ( condition ) loop
----------------------------------------------------------------------------end loop;
Example
_______
Declare
v_counter

number(3) := 0;

begin
while ( v_counter <= 10) loop
dbms_output.put_line(v_counter);
v_counter := v_counter +1 ;
end loop;
end;
_____________________________________________________________________________
2.3) for loop
______________
syntax
_______
for variable_name

in lower_limit

..

upper_limit

loop

---------------------------------------------------------------------end loop;
Example
_________
begin
for v_counter in 1..10 loop
dbms_output.put_line( v_counter );
end loop;
end;
begin
for v_counter in reverse 1..10 loop
dbms_output.put_line( v_counter );
end loop;
end;
________________________________________________________________________________
______
/////////////////////////////////////END/////////////////////////////////////

You might also like