Thanks to visit codestin.com
Credit goes to www.scribd.com

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

For While

The document explains the FOR LOOP and WHILE LOOP control structures in PL/SQL, detailing their syntax, flow of control, and special characteristics. It includes examples of both types of loops, demonstrating how to iterate through a range of values and how to use the CONTINUE and EXIT statements to control loop execution. Additionally, it describes the REVERSE FOR LOOP, which allows iteration in reverse order while maintaining ascending range bounds.

Uploaded by

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

For While

The document explains the FOR LOOP and WHILE LOOP control structures in PL/SQL, detailing their syntax, flow of control, and special characteristics. It includes examples of both types of loops, demonstrating how to iterate through a range of values and how to use the CONTINUE and EXIT statements to control loop execution. Additionally, it describes the REVERSE FOR LOOP, which allows iteration in reverse order while maintaining ascending range bounds.

Uploaded by

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

A FOR LOOP is a repetition control structure that allows you to efficiently

write a loop that needs to execute a specific number of times.

Syntax
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;
END LOOP;
Following is the flow of control in a For Loop −
 The initial step is executed first, and only once. This step allows
you to declare and initialize any loop control variables.
 Next, the condition, i.e., initial_value .. final_value is evaluated. If
it is TRUE, the body of the loop is executed. If it is FALSE, the
body of the loop does not execute and the flow of control jumps
to the next statement just after the for loop.
 After the body of the for loop executes, the value of the counter
variable is increased or decreased.
 The condition is now evaluated again. If it is TRUE, the loop
executes and the process repeats itself (body of loop, then
increment step, and then again condition). After the condition
becomes FALSE, the FOR-LOOP terminates.
Following are some special characteristics of PL/SQL for loop −
 The initial_value and final_value of the loop variable or counter
can be literals, variables, or expressions but must evaluate to
numbers. Otherwise, PL/SQL raises the predefined exception
VALUE_ERROR.
 The initial_value need not be 1; however, the loop counter
increment (or decrement) must be 1.
 PL/SQL allows the determination of the loop range dynamically at
run time.

Example
DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
Reverse FOR LOOP Statement
By default, iteration proceeds from the initial value to the final value,
generally upward from the lower bound to the higher bound. You can reverse
this order by using the REVERSE keyword. In such case, iteration proceeds
the other way. After each iteration, the loop counter is decremented.
However, you must write the range bounds in ascending (not descending)
order. The following program illustrates this −
DECLARE
a number(2) ;
BEGIN
FOR a IN REVERSE 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/

A WHILE LOOP statement in PL/SQL programming language repeatedly executes a


target statement as long as a given condition is true.

DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);
a := a + 1;
END LOOP;
END;
/

1. DECLARE
2. VAR1 NUMBER;
3. VAR2 NUMBER;
4. BEGIN
5. VAR1:=200;
6. VAR2:=1;
7. WHILE (VAR2<=10)
8. LOOP
9. DBMS_OUTPUT.PUT_LINE (VAR1*VAR2);
10. VAR2:=VAR2+1;
11. END LOOP;
12. END;

Continue

BEGIN

FOR n_index IN 1 .. 10

LOOP

-- skip odd numbers

IF MOD( n_index, 2 ) = 1 THEN

CONTINUE;

END IF;

DBMS_OUTPUT.PUT_LINE( n_index );

END LOOP;

END;

Continue when

BEGIN

FOR n_index IN 1 .. 10

LOOP

-- skip even numbers

CONTINUE

WHEN MOD( n_index, 2 ) = 0;

DBMS_OUTPUT.PUT_LINE( n_index );

END LOOP;

END;

Exit
DECLARE
a number(2) := 10;
BEGIN
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
IF a > 15 THEN
-- terminate the loop using the exit statement
EXIT;
END IF;
END LOOP;
END;
/

Exit when

DECLARE
a number(2) := 10;
BEGIN
-- while loop execution
WHILE a < 20 LOOP
dbms_output.put_line ('value of a: ' || a);
a := a + 1;
-- terminate the loop using the exit when statement
EXIT WHEN a > 15;
END LOOP;
END;
/

You might also like