0% found this document useful (0 votes)
79 views4 pages

Class 4

The document contains examples of PL/SQL code demonstrating control flow statements including IF/THEN/ELSE statements, CASE statements, loops (WHILE and FOR), and procedures to calculate properties of a circle and print numbers in ascending and descending order.

Uploaded by

Venu D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views4 pages

Class 4

The document contains examples of PL/SQL code demonstrating control flow statements including IF/THEN/ELSE statements, CASE statements, loops (WHILE and FOR), and procedures to calculate properties of a circle and print numbers in ascending and descending order.

Uploaded by

Venu D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//programe to find find diameter,area and circumference of circle

DECLARE
-- constant declaration
pi constant number := 3.141592654;
-- other declarations
radius number(5,2);
dia number(5,2);
circumference number(7, 2);
area number (10, 2);
BEGIN
-- processing
radius := 9.5;
dia := radius * 2;
circumference := 2.0 * pi * radius;
area := pi * radius * radius;
-- output
dbms_output.put_line('Radius: ' || radius);
dbms_output.put_line('Diameter: ' || dia);
dbms_output.put_line('Circumference: ' || circumference);
dbms_output.put_line('Area: ' || area);
END;

//like switch in c language

CASE Statement:

CASE selector
WHEN value-1
THEN statement-1;
WHEN value-2
THEN statement-2;
ELSE
statement-3;
END CASE

DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Well done');
when 'D' then dbms_output.put_line('You passed');
when 'F' then dbms_output.put_line('Better try again');
else dbms_output.put_line('No such grade');
END CASE;
END;

Searched CASE Statement:

CASE
WHEN condition-1 THEN
statement-1;
WHEN condition-2 THEN
statement-2;
ELSE
statement-3;
END CASE;

SQL> DECLARE
2 a number := 3;
3 BEGIN
4 CASE
5 WHEN a = 1 THEN
6 DBMS_OUTPUT.PUT_LINE('value 1');
7 WHEN a = 2 THEN
8 DBMS_OUTPUT.PUT_LINE('value 2');
9 WHEN a = 3 THEN
10 DBMS_OUTPUT.PUT_LINE('value 3');
11 ELSE
12 DBMS_OUTPUT.PUT_LINE('no matching CASE found');
13 END CASE;
14 END;
15 /

value 3

PL/SQL procedure successfully completed.

//if then ex:

SQL> set serveroutput on


SQL> declare
2 a number(2):=&a;
3 begin
4 if(a>=20)then
5 dbms_output.put_line(a||'is greater Than 20');
6 end if;
7 end;
8 /
Enter value for a: 34
old 2: a number(2):=&a;
new 2: a number(2):=34;
34is greater Than 20

PL/SQL procedure successfully completed.

ex://if else example


SQL> DECLARE
2 a number(3) := 100;
3 BEGIN
4 IF( a < 20 ) THEN
5 dbms_output.put_line('a is less than 20 ' );
6 ELSE
7 dbms_output.put_line('a is not less than 20 ' );
8 END IF;
9 dbms_output.put_line('value of a is : ' || a);
10 END;
11 /
a is not less than 20
value of a is : 100

PL/SQL procedure successfully completed.


set serveroutput on
DECLARE
a number(3) := 100;
BEGIN
IF ( a = 10 ) THEN
dbms_output.put_line('Value of a is 10' );
ELSIF ( a = 20 ) THEN
dbms_output.put_line('Value of a is 20' );
ELSIF ( a = 30 ) THEN
dbms_output.put_line('Value of a is 30' );
ELSE
dbms_output.put_line('None of the values is matching');
END IF;
dbms_output.put_line('Exact value of a is: '|| a );
END;
/
None of the values is matching
Exact value of a is: 100

PL/SQL procedure successfully completed.


ex:print 1 to 10 nos
SQL> DECLARE
2 no NUMBER := 1;
3 BEGIN
4 WHILE no <= 10 LOOP
5 DBMS_OUTPUT.PUT_LINE( no);
6 no := no + 1;
7 END LOOP;
8 END;
9 /

ex:print 1 to 9 nos
DECLARE
no NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ( no);
no := no +1;
IF no = 10 THEN
EXIT;
END IF;
END LOOP;
END;

DECLARE
no NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ( no);
no := no +1;
IF no = 10 THEN
EXIT;
END IF;
END LOOP;
END;
//print 1 to 5 nos in asc
BEGIN
FOR no IN 1 .. 5 LOOP
DBMS_OUTPUT.PUT_LINE(no);
END LOOP;
END;
/
//print 1 to 5 nos in desc
BEGIN
FOR no IN reverse 1 .. 5 LOOP
DBMS_OUTPUT.PUT_LINE(no);
END LOOP;
END;
/

You might also like