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

Oracle PL SQL 4

The document discusses decision structures and loop structures in Oracle PL/SQL including the IF-THEN statement, CASE statement, WHILE loop, and FOR loop. Examples are provided for each structure to illustrate their usage.

Uploaded by

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

Oracle PL SQL 4

The document discusses decision structures and loop structures in Oracle PL/SQL including the IF-THEN statement, CASE statement, WHILE loop, and FOR loop. Examples are provided for each structure to illustrate their usage.

Uploaded by

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

ORACLE PL/SQL

Use decision structures: IF-THEN and CASE

PL/SQL: if Statement

• Is used when there is only a single condition to be tested.

• If the result of the condition is TRUE then certain specified action will be performed
otherwise if it is FALSE then no action is taken and the control of program will just move out
of the if code block.

• An if statement lets your program know whether or not it should execute a block of code.

• if condition statement flow chart

• Example:

• if <test_condition> then

• body of action

• end if;

• It selects only one option out of the multiple available options.

• It uses a selector for this purpose.


• This selector can be a variable, function or procedure that returns some value and on the
basis of the result one of the case statements is executed.

• If all the cases fail then the else case is executed.

CASE selector

when value1 then Statement1;

when value2 then Statement2;

...

else statement;

end CASE;

Example:

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;

• This allows to specify two statements or two set of statements, dependent on a condition
such that when the condition is true then one set of statements is executed.

• If the condition is false then the other set of statements is executed.

if <test_condition> then

statement 1/set of statements 1

else

statement 2/set of statements 2

end if;
Use loop structures: FOR and WHILE

• It provides a way of repeating a particular part of any program or any code statement as
many times as required.

While Loop

• It is an entry-controlled loop

• Before entering in a while loop first the condition is tested, if the condition is TRUE the
statement or a group of statements get executed.

• If the condition is FALSE the control will move out of the while loop.

WHILE <test_condition> LOOP

<action>

END LOOP;

• It provides a way of repeating a particular part of any program or any code statement as
many times as required.

While Loop

• It is an entry-controlled loop

• Before entering in a while loop first the condition is tested, if the condition is TRUE the
statement or a group of statements get executed.
• If the condition is FALSE the control will move out of the while loop.

WHILE <test_condition> LOOP

<action>

END LOOP;

• PL/SQL: For Loop

• This loop is used when some statements in PL/SQL code block are to be repeated for a fixed
number of times.

• Define a counter variable which decides how many time the loop will be executed based on a
starting and ending value provided at the beginning of the loop.

• The for loop automatically increments the value of the counter variable by 1 at the end of
each loop cycle.

FOR counter_variable IN start_value..end_value LOOP

statement to be executed

END LOOP;

Practice exercises

https://devgym.oracle.com/pls/apex/dg/workout/case-in-pl-sql.html

To install oracle database

https://youtu.be/4qJJxQuHLC4?si=KgeRz_XAZqKRPJFq

To download HR schema

You might also like