Download presentation
Presentation is loading. Please wait.
Published bySimon Sullivan Modified over 9 years ago
1
PL/SQL Loops
2
Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining number, character or date expressions with a comparison operator Examples: v_name1 = v_name2 v_name != ‘Ali’ v_cgpa between 2.00 and 4.00 v_cgpa > 2.50 v_name=‘Ali’ and v_deptno=35 and v_cgpa between 2.00 and 4.00
3
Using NULL values in Logical Conditions MUSTYou MUST handle null values with the IS NULL or IS NOT NULL operator. Any arithmetic expression containing a null value evaluates to NULL. Concatenated expressions with null values treat null values as an empty string.
4
Logic Tables NOT TRUE FALSE NULL OR TRUE FALSE NULL TRUEFALSENULL FALSE TRUE NULL AND TRUE FALSE NULL TRUEFALSENULL TRUE NULL FALSE TRUE FALSE NULL Boolean conditions with Logical operators
5
Boolean Conditions V_REORDER_FLAGV_AVAILABLE_FLAGV_FLAG TRUE TRUEFALSE NULLTRUE NULLFALSE v_flag := v_reorder_flag AND v_available_flag; TRUEFALSENULLFALSE What is the value of V_FLAG in each case?
6
Iterative Control: LOOP Statements Loops repeat a statement or sequence of statements multiple times. There are three loop types: –Basic loop –FOR loop –WHILE loop
7
Main uses of Loop Types Use Basic loop: to provide repetitive actions without overall conditions FOR loops: to provide iterative control of actions based on a count WHILE: loops to provide iterative control of actions based on a condition EXIT statement : to terminate loops depending on your programming logic
8
Basic Loop LOOP statement1;... EXIT [WHEN condition]; END LOOP; LOOP statement1;... EXIT [WHEN condition]; END LOOP; where:conditionis a Boolean variable or expression (TRUE, FALSE, or NULL); where:conditionis a Boolean variable or expression (TRUE, FALSE, or NULL); -- delimiter showing beginning of the loop -- statements -- EXIT statement -- delimiter showing end of the loop Syntax If the exit statement of a basic loop is missing, you have an infinite loop
9
The EXIT Statement You can terminate a loop using the EXIT statement. Control passes to the next statement after the END LOOP statement. You can issue EXIT either as an action within an IF statement or as a standalone statement within the loop. The EXIT statement must be placed inside a loop. –You can attach a WHEN clause to allow conditional termination of the loop. When the EXIT statement is encountered, the condition in the WHEN clause is evaluated. –If the condition yields TRUE, the loop ends and control passes to the next statement after the loop. A basic loop can contain multiple EXIT statements.
10
Basic Loop DECLARE v_counterNUMBER(2) := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is ‘||v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; DECLARE v_counterNUMBER(2) := 0; BEGIN LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is ‘||v_counter); v_counter := v_counter + 1; EXIT WHEN v_counter > 10; END LOOP; END; Example: Common mistake: failing to increment v_counter will result in an infinite loop!
11
FOR Loop FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2;... END LOOP; FOR counter in [REVERSE] lower_bound..upper_bound LOOP statement1; statement2;... END LOOP; Syntax Use a FOR loop to shortcut the test for the number of iterations. You do not have to declare the index; it is declared implicitly.
12
Counter : an implicitly declared integer whose value automatically increases or decreases by 1 on each iteration of the loop until the upper or lower bound is reached REVERSE : causes the index to decrement with each iteration from the upper bound to the lower bound –The lower bound is still referenced first. –If the lower bound of the loop range evaluates to a larger integer than the upper bound, the sequence of statements will not be executed. FOR Loop
13
Guidelines –Reference the counter within the loop only; it is undefined outside the loop. –Use an expression to reference the existing value of a counter. –Do not reference the counter as the target of an assignment.
14
FOR Loop BEGIN FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current value is ‘|| i); END LOOP; END; BEGIN FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current value is ‘|| i); END LOOP; END; Print numbers 1 to 10 on the screen Example
15
WHILE Loop WHILE condition LOOP statement1; statement2;... END LOOP; WHILE condition LOOP statement1; statement2;... END LOOP; Condition is evaluated at the beginning of each iteration. Syntax Use the WHILE loop to repeat statements while a condition is TRUE.
16
You can use the WHILE loop to repeat a sequence of statements until the controlling condition is no longer TRUE. –The condition is evaluated at the start of each iteration. –The loop terminates when the condition is FALSE. If the condition is FALSE at the start of the loop, then no further iterations are performed. If the condition is NULL, the loop is bypassed and control passes to the next statement. If the variables involved in the conditions do not change during the body of the loop, then the condition remains TRUE and the loop does not terminate—infinite loop. WHILE Loop
17
DECLARE v_counterNUMBER(2) := 1; BEGIN WHILE v_counter <= 10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is’ || v_counter); v_counter := v_counter + 1; END LOOP; END; DECLARE v_counterNUMBER(2) := 1; BEGIN WHILE v_counter <= 10 LOOP DBMS_OUTPUT.PUT_LINE(‘Current Value is’ || v_counter); v_counter := v_counter + 1; END LOOP; END; Print numbers 1 to 10 on the screen Example
18
Nested Loops and Labels Nest loops to multiple levels. –You can nest FOR, WHILE, and basic loops within one another –The termination of a nested loop does not terminate the enclosing loop unless an exception was raised Use labels to distinguish between blocks and loops. Exit the outer loop with the EXIT statement referencing the label.
19
A label is placed before a statement, either on the same line or on a separate line. Label loops by placing the label before the word LOOP within label delimiters ( >). If the loop is labeled, the label name can optionally be included after the END LOOP statement for clarity. Nested Loops and Labels
20
... BEGIN > LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; > LOOP... EXIT Outer_loop WHEN total_done = 'YES'; -- Leave both loops EXIT WHEN inner_done = 'YES'; -- Leave inner loop only... END LOOP Inner_loop;... END LOOP Outer_loop; END;... BEGIN > LOOP v_counter := v_counter+1; EXIT WHEN v_counter>10; > LOOP... EXIT Outer_loop WHEN total_done = 'YES'; -- Leave both loops EXIT WHEN inner_done = 'YES'; -- Leave inner loop only... END LOOP Inner_loop;... END LOOP Outer_loop; END; Using the label of the outer loop in the exit statement terminates the outer loop as well as the inner loop.
21
SummarySummary This week we learned about Loops –Basic loop –FOR loop –WHILE loop –EXIT statement Next Lesson we will learn how to use SELECT and DML statements inside PL/SQL blocks.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.