You are on page 1of 4

Control Statements

--> If :
Syntax:

if (condtion) then
Statements;
else
Statements;
end if;

--> Greater of two nos


declare
a number:=&a;
b number:=&b;
begin
if a>b then
dbms_output.put line('a i greater');
else
dbms_output.put line('b is greater');
endif;
end;
--> Greater Of Three Nos.
declare
a number:=&a;
b number:=&b;
c number:=&c;
begin
if (a>b and a>c) then
dbms_output.put line('a is greater');
elsif (b>c) then
dbms_output.put line('b is greater');
else
dbms_output.put line('c is greater');
end if;
end;

-------------------------------------------------------------------------------------> Nested-If:
Syntax: if(condition) then
Statements;
elsif(condition) then
Statements;
elsif(condition) then
Statements;
else
Statements;
end if;

declare
a number:=&a;
b number:=&b;

c number:=&c;
begin
if (a>b) then
if (a>c) then
dbms_output.put_line('a is greater');
end if;
elsif (b>c) then
dbms_output.put_line('b is greater');
else
dbms_output.put_line('c is greater');
end if;
end;
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------While :

Syntax: while (condition) loop


Statements;
end loop;

--> Display All Even Nos. Upto Given Range


Declare
range number:=⦥
i
number;
Begin
i:=2;
while (i<=range) loop
dbms_output.put_line(i);
i:=i+2;
end loop;
End;
--> Finding The Factorial Of a Number
Declare
no number:=&no;
fact number:=1;
Begin
while (no>1) loop
fact:=fact*no;
no:=no-1;
end loop;
dbms_output.put_line('Factorial Of a Given Number Is =' ||fact);
End;
-------------------------------------------------------------------------------------------For :
Syntax: For var in (rev) start_val..end_val loop
Statements;
End loop
--> Display Nos. Upto Given Range.

Declare
i number(2);
Begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
End;
--> Display Nos. In Reverse Order.
Declare
i number(2);
Begin
for i in reverse 1..10 loop
dbms_output.put_line(i);
end loop
End;
-------------------------------------------------------------------------------------------Goto :
Syntax: .....................
.....................
goto xyz;
.....................
<<xyz>>
Statements;
.....................
--> Write a Pl/Sql Program Using Goto.
Declare
i number:=1;
Begin
for i in 1..10 loop
if (i=5) then
goto exit;
end if;
dbms_output.put_line(i);
end loop;
<<exit>>
dbms_output.put_line('Quiting From The Program');
End;
/
--> Give a Bonus To An Employee If The Criteria Is Satisfied.
Declare
Name varchar(15):=&name;
No number(3):=&no;
Sal Number(6):=&sal;
Doj Date:=&date;
Cdate Date:=&cdate;
Begin
if((Cdate-Doj)>=2) then
Sal:=Sal+2500;
dbms_output.put_line('Bonus Is Added To Salary');
goto Det;
else

dbms_output.put_line('No Bonus');
goto Det;
end if;
<<Det>>
dbms_output.put_line('EmployeeName '||Name);
dbms_output.put_line('EmployeeNumber '||No);
dbms_output.put_line('Salary '||Sal);
dbms_output.put_line('Date Of Join '||Doj);
End;
----------------------------------------------------------------------------------------------> Selected case:
Syntax: Case Var_Name
When Val1 Then
Statements;
When Val2 Then
Statements;
.......................
End Case;
Declare
a number:=&a;
b number:=&b;
c number;
choice number;
Begin
dbms_output.put_line('1.ADD, 2.SUB
choice:=&choice;

3.DIV

4.MUL');

case choice
when
1 then
c:=a+b;
when 2 then
c:=a-b;
when 3 then
c:=a/b;
when 4 then
c:=a*b;
end case;
dbms_output.put_line('Result Is :=' ||c);
End;
/
--------------------------------------------------------------------------------------------

You might also like