While-loop - iterative control, PL-SQL Programming

WHILE-LOOP

The WHILE-LOOP statement relates a condition with the series of statements enclosed by the keywords LOOP and END LOOP, as shown:

WHILE condition LOOP
sequence_of_statements
END LOOP;

Before each of the iteration of the loop, the condition is computed. If the condition is true, then the series of statements is executed, then the control resumes at the top of the loop. When the condition is false or null, the loop is then bypassed and control passes to the next statement. An illustration is shown below:

WHILE total <= 25000 LOOP
...
SELECT sal INTO salary FROM emp WHERE...
total := total + salary;
END LOOP;

The number of iterations depends on the condition and is not known until the loop done. The condition is tested at the top of the loop, so the series might execute zero times. In the last illustration, if the initial value of total is bigger than 25000, the condition is false and the loop is bypassed.

A few languages have a LOOP UNTIL or REPEAT UNTIL structure, that tests the condition at the bottom of the loop rather than at the top. So, the sequence of the statements is executed at least once. The PL/SQL has no such structure, but you can easily build one, as shown:

LOOP
sequence_of_statements
EXIT WHEN boolean_expression;
END LOOP;

To make sure that a WHILE loop executes at least once, then use an initialized Boolean variable in the condition which is as shown below:

done := FALSE;
WHILE NOT done LOOP
sequence_of_statements
done := boolean_expression;
END LOOP;


The statement inside the loop should assign a new value to the Boolean variable. Or else, you have an infinite loop. For illustration, the following LOOP statements are logically equal:

WHILE TRUE LOOP | LOOP
... | ...
END LOOP; | END LOOP;

Posted Date: 10/3/2012 5:50:32 AM | Location : United States







Related Discussions:- While-loop - iterative control, Assignment Help, Ask Question on While-loop - iterative control, Get Answer, Expert's Help, While-loop - iterative control Discussions

Write discussion on While-loop - iterative control
Your posts are moderated
Related Questions
I have a Pascal Source file that needs to be compiled into a Service. In addition, there are various functions (Pascal Procedures I guess) that need to be created to Read and Write

Use the NOCOPY Compiler Hint By default, the OUT and IN OUT parameters are passed by the value i.e. the value of an IN OUT actual parameter is copied into the corresponding fo

Parameter Modes   To define the behavior of formal parameters you use the parameter modes. The 3 parameter modes, IN, OUT, & IN OUT, can be used with any subprogram. Though, a

Use the PLS_INTEGER Datatype When you require to declare an integer variable, use the datatype PLS_INTEGER that is the most efficient numeric type. That is as the PLS_INTEGER

Consider the following set of database tables (same tables from Assignment 6-1). Please take note of foreign keys (most of them carry the same names as the corresponding primary ke

Row Counterparts of Table Operators SQL does not have counterparts tuple rename, tuple projection, tuple extension, tuple join and tuple compose. To obtain the same effects as

Table Represents an Extension - SQL It describes how each tuple in a relation represents a true instantiation of some predicate and each true instantiation is represented by s

Effects of NULL for union - SQL The treatment of NULL in invocations of EXCEPT is as for UNION. This is different from its treatment in those of NOT IN and quantified compari

Fetching from a Cursor Variable The FETCH statement retrieve rows one at a time from the product set of a multi-row query. The syntax for the same is as shown: FETCH {curso

Using RENAME in combination with JOIN - SQL Example gives pairs of ids of students having the same name, by joining two renamings of IS_CALLED. Example gives an equivalent ex