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
Substitution and Instantiation - SQL It shows how NULL might appear in substitution for a parameter of a predicate and how it might thus participate in instantiation of that p

Transaction Visibility As the figure shows, the changes made by an autonomous transaction become visible to another transaction whenever the autonomous transaction commits. Th

UNION ALL - SQL Further varieties of UNION arise when we replace the key word DISTINCT by ALL in any of the foregoing examples, as in Example. ALL specifies that if row r appe

Bitmap Join Indexes - This feature will increase the performance and detains the size and format of your databases in data Character Semantics and Globalization -This featur

Using a join on 3 tables, select 5 columns and 10 rows from the 3 tables without the use of a Cartesian product Query: SELECT E.LAST_NAME, E.FIRST_NAME, S.BUILDING, S.BRAN

Components of an Object Type: An object type encapsulates the operations and data. Therefore, you can declare the methods and attributes in an object type specification, but no

Expressions   An expression is a randomly complex combination of the constants, variables, literals, operators, & function calls. The simplest expression is the single variabl


Benefit of the dynamic SQL: This part shows you how to take full benefit of the dynamic SQL and how to keep away from some of the common pitfalls. Passing the Names of Sc

Manipulating Collections Within PL/SQL, the collections add procedural power and flexibility. The biggest benefit is that your program can compute subscripts to process the spec