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
Updating Variables For assignment, SQL uses the key word SET, as in SET X = X + 1 (read as "set X equal to X+1") rather than X: = X + 1 as found in many computer languages.

THEORY OF SPONTANEOUS GENERATION - ABIOGENESIS OR AUTOGENESIS - According to this theory, the existing living communities have originated from non-living organic matter with

Perform the following queries on the Hospital1.DB using SQL Anywhere (START EARLY!). a. Which patients have purchased the drug "Tylenol"? List the names and addresses. Arrange the

Name Resolution   During the compilation, the PL/SQL compiler relates identifiers like the name of a variable with an address or memory location, actual value, or datatype. Th

Create a Oracle procedure to produce vertical output format when selecting rows from a database table.

Logical Operators The logical operators AND, NOT, and OR follow the tri-state logic shown in table below. The AND and OR are binary operators; NOT is a unary operator.

Project Description: I have two types of data sources. One that is a list in SharePoint and another that is an access desktop database. The access desktop database is fairly com

BETWEEN Operator The operator BETWEEN, tests whether the value lies in a specified series. That means "greater than or equivalent to low value and less than or equivalent to hig

i NEED THE QURIES TO SOME OF THE PROBLEMS USING plsql

SQL Functions The PL/SQL uses all the SQL functions involving the following aggregate functions that summarize the whole columns of the Oracle data: GROUPING, AVG, COUNT, STDDE