Professional Documents
Culture Documents
Control Statements
Control Statements
--> If :
Syntax:
if (condtion) then
Statements;
else
Statements;
end if;
-------------------------------------------------------------------------------------> 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 :
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;
/
--------------------------------------------------------------------------------------------