FOR-LOOP Assignment Help

Assignment Help: >> Iterative Control - FOR-LOOP

FOR-LOOP:

Whereas the number of iterations for a WHILE loop is unknown until the loop completes and the number of iterations for a FOR loop is known before the loop is entered. A FOR loops iterate over a specified range of integers. The range is categories of an iteration scheme that is enclosed within the keywords FOR and LOOPS. The syntax is as follows:

FOR counter IN [REVERSE] lower_bound..higher_bound LOOP

sequence_of_statements;

END LOOP;

The range is evaluated when the FOR loop is first entered and is never re-evaluated. As the next instance displays, the sequence of statements is executed once for each integer in the range. After the each iteration, the loop counter is incremented. The given example shows the reversal of a string:

Example

DECLARE

S VARCHAR2(20):='&s';

S1 VARCHAR2(20);

BEGIN

FOR I IN 1..LENGTH(S)

LOOP

S1:=S1||SUBSTR(S,-I,1);

END LOOP;

DBMS_OUTPUT.PUT_LINE(s1);

END;

In the given example, a string is accepted. The number of iterations is fixed via the length of the string. Every character in the string is extracted and assigned to the variable S1. The variable is known as a counter variable and it cannot be assigned or declared.

Example

DECLARE

s VARCHAR2(20):='&s';

s1 VARCHAR2(20);

BEGIN

FOR i IN 1..LENGTH(s)

LOOP

s1:=s1||SUBSTR(s,-i,1);

            DBMS_OUTPUT.PUT_LINE('The counter value is '||i);

END LOOP;

DBMS_OUTPUT.PUT_LINE(s1);

END;

This code displays the value of the counter variable.

By default iteration proceeds upward from the lower bound to the higher bound. Still, the keyword REVERSE is used and iteration proceeds downward from the higher bound to the lower bound, as the given below example shows. After each iteration the loop counter is decrease.

FOR i IN 1..3 LOOP  -- assign the values 1,2,3 to i

sequence_of_statements; -- executes three times

END LOOP;

However, the range bounds are to be written in ascending (not descending) order. Inside a FOR loop and the loop counter can be referenced such as a constant. So, the loop counter can appear in expressions but it cannot be assigned values, as the given example displays:

FOR ctr IN 1..10 LOOP

...

IF NOT finished THEN

INSERT INTO ... VALUES (ctr, ...); -- legal

factor := ctr * 2;          -- legal

ELSE

ctr := 10;          -- illegal

END IF;

END LOOP;

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd