CIS395 - BMCC - Spring 2018 04/25/2018 Lab 09: Advanced SQL A - Use Simple Loops with EXIT Conditions In this exercise, you use the EXIT condition to terminate a simple loop, and a special variable, v_counter, which keeps count of the loop iterations. The value of v_counter is incremented and displayed on the screen with each iteration of the loop. 1- Execute the following PL/SQL script: v_counter NUMBER := 0; -- increment loop counter by one -- if EXIT condition yields TRUE exit the loop IF v_counter = 5 THEN EXIT; DBMS_OUTPUT.PUT_LINE ('Done...'); 2- How many times did the loop execute? 3- What is the EXIT condition for this loop? 4- Rewrite and execute the script using the EXIT WHEN condition instead of the EXIT condition so that it produces the same result. To accomplish this task, replace the IF statement with EXIT WHEN v_counter = 5; statement. B - Use WHILE Loops In this exercise, you use a WHILE loop to calculate the sum of the integers between 1 and 10. 5- Execute the following PL/SQL script:
v_counter NUMBER := 1; v_sum NUMBER := 0; WHILE v_counter <= 10 v_sum := v_sum + v_counter; DBMS_OUTPUT.PUT_LINE ('Current sum is: ' v_sum); -- increment loop counter by one DBMS_OUTPUT.PUT_LINE ('The sum of integers between 1 and 10 is: ' v_sum); 6- What is the test condition for this loop? 7- How many times did the loop execute? 8- How many times will the loop execute a) if v_counter is initialized to 0? b) if v_counter is initialized to 10? 9- Rewrite and execute the script to calculate the sum of the even integers between 1 and 100. C - FOR Loops 10- Execute the following PL/SQL script: FOR v_counter IN 1..5 In this example, there is no declaration section for the PL/SQL block because the only variable used, v_counter, is the loop counter. Numbers 1..5 specify the range of the integer numbers for which this loop is executed. USING THE REVERSE OPTION IN THE : The next example demonstrates the usage of the IN REVERSE option for the loop. 11- Execute the following PL/SQL script: FOR v_counter IN REVERSE 1..5 PREMATURE TERMINATION OF THE :
12- Execute the following example of a FOR loop that uses the EXIT WHEN condition. This condition causes the loop to terminate prematurely. FOR v_counter IN 1..5 EXIT WHEN v_counter = 3; D - Use Numeric FOR Loops with the IN Option In this exercise, you use a numeric FOR loop to calculate a factorial of 10 (10! = 1*2*3...*10). 13- Execute the following PL/SQL script: v_factorial NUMBER := 1; FOR v_counter IN 1..10 v_factorial := v_factorial * v_counter; DBMS_OUTPUT.PUT_LINE ('Factorial of ten is: ' v_factorial); 14- Rewrite and execute the above script using the IN REVERSE option. E CONTINUE Statement The CONTINUE statement exits the current iteration of a loop and transfers control to the next iteration. 15- Execute the following PL/SQL script: v_counter NUMBER := 0; DBMS_OUTPUT.PUT_LINE ('before continue condition, v_counter = ' v_counter); -- if CONTINUE condition yields TRUE, exit the current -- iteration and pass control to the next iteration IF v_counter < 4 THEN CONTINUE; DBMS_OUTPUT.PUT_LINE ('after continue condition, v_counter = ' v_counter); -- if EXIT condition yields TRUE exit the loop IF v_counter = 5 THEN EXIT; DBMS_OUTPUT.PUT_LINE ('Done...');
F - Nested Loops We have explored three types of loops: simple loops, WHILE loops, and numeric FOR loops. Any of these three types of loops can be nested inside one another. For example, a simple loop can be nested inside a WHILE loop, and vice versa. 16- Execute the following PL/SQL script: v_counter1 INTEGER := 0; v_counter2 INTEGER; WHILE v_counter1 < 3 DBMS_OUTPUT.PUT_LINE ('Counter1 = ' v_counter1); v_counter2 := 1; DBMS_OUTPUT.PUT_LINE ('------------Counter2 = ' v_counter2); v_counter2 := v_counter2 + 1; EXIT WHEN v_counter2 > 3; v_counter1 := v_counter1 + 1; 17- Execute the following PL/SQL script: FOR i IN 1..10 FOR j IN 1..i -- append an asterisk at the end of the current line DBMS_OUTPUT.PUT('* '); -- start a new line DBMS_OUTPUT.PUT_LINE(''); 18- How many times did the outer loop execute? 19- How many times did the inner loop execute? 20- Rewrite and execute the script using the REVERSE option for the outer loop. 21- Using any types of nested loops, create a PL/SQL script that produces the following output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8
22- Using any types of nested loops, create a PL/SQL script that produces the following output: 10 15 20 25 30 35 40 45 12 18 24 30 36 42 48 54 14 21 28 35 42 49 56 63 16 24 32 40 48 56 64 72 18 27 36 45 54 63 72 81 20 30 40 50 60 70 80 90 22 33 44 55 66 77 88 99 23- Using any types of nested loops, create a PL/SQL script that produces the following output: 1 22 333 4444 55555 666666