In PL/SQL there is 3 loop. It is a For loop, while loop, and do-while. This post, i will discuss about PL/SQL While-Loop. The structure of this loop, the program will enter the loop body is given only if the condition is true. Checking process occurs when the program entered the loop body. Almost every language has a procedural structure of this loop. General syntax:
WHILE condition LOOP
statemen_1;
...
END LOOP;
example: display the text I Learning PL / SQL.
DECLARE
Integer J;
BEGIN
A: = 1;
WHILE (J <= 10) LOOP
dbms_output.put_line ('I Learning PL / SQL');
J: = J +1;
END LOOP;
END:
ket:
before entering into the body of loop variable J has a value which is 1, then be checked against that value is less than or equal to 10 or not. If yes, then the program enters the process of repetition. And vice versa. Since the condition 1 <= 10 TRUE, the statements in row 6 will be executed. Then, the value of J will increase to 2 because the statements in row 7. An iterative process will stop if the variable A is worth 11 because the condition J <= 10 was worth FALSE.
example: calculate the value of powers of 2 to the power 6
DECLARE
BIL integer: = 2;
RESULTS integer: = 1;
Integer K;
BEGIN
K: = 0;
WHILE K <6 LOOP
RESULT: = RESULT * BIL;
K: = K +1;
END LOOP;
dbms_output.put_line ('Results from the 2 ^ 6 =' | | RESULT);
END;
example: 5x10 multiplication without multiplication operator (*).
DECLARE
BIL integer: = 5;
RESULTS integer: = 0;
K integer: = 1;
BEGIN
LIMITS: = BIL;
WHILE K <= 10 LOOP
RESULT: = RESULT + BIL;
K: = K +1;
END LOOP;
dbms_output.put_line ('Results from 5 X 10 =' | | RESULT);
END;
WHILE condition LOOP
statemen_1;
...
END LOOP;
example: display the text I Learning PL / SQL.
DECLARE
Integer J;
BEGIN
A: = 1;
WHILE (J <= 10) LOOP
dbms_output.put_line ('I Learning PL / SQL');
J: = J +1;
END LOOP;
END:
ket:
before entering into the body of loop variable J has a value which is 1, then be checked against that value is less than or equal to 10 or not. If yes, then the program enters the process of repetition. And vice versa. Since the condition 1 <= 10 TRUE, the statements in row 6 will be executed. Then, the value of J will increase to 2 because the statements in row 7. An iterative process will stop if the variable A is worth 11 because the condition J <= 10 was worth FALSE.
example: calculate the value of powers of 2 to the power 6
DECLARE
BIL integer: = 2;
RESULTS integer: = 1;
Integer K;
BEGIN
K: = 0;
WHILE K <6 LOOP
RESULT: = RESULT * BIL;
K: = K +1;
END LOOP;
dbms_output.put_line ('Results from the 2 ^ 6 =' | | RESULT);
END;
example: 5x10 multiplication without multiplication operator (*).
DECLARE
BIL integer: = 5;
RESULTS integer: = 0;
K integer: = 1;
BEGIN
LIMITS: = BIL;
WHILE K <= 10 LOOP
RESULT: = RESULT + BIL;
K: = K +1;
END LOOP;
dbms_output.put_line ('Results from 5 X 10 =' | | RESULT);
END;
That was a brief explanation about PL/SQL While-Loop. If there are errors or omissions please justified or added. Hope usefull.
0 komentar
Posting Komentar