Lab 8.2 Exercises

8.2.1 Use WHILE Loops

In this exercise, you will use a WHILE loop to calculate the sum of the integers between 1 and 10.

Create the following PL/SQL script:

 -- ch08_3a.sql, version 1.0 SET SERVEROUTPUT ON DECLARE    v_counter BINARY_INTEGER := 1;    v_sum NUMBER := 0; BEGIN    WHILE v_counter <= 10 LOOP       v_sum := v_sum + v_counter;       DBMS_OUTPUT.PUT_LINE ('Current sum is: 'v_sum);       -- increment loop counter by one       v_counter := v_counter + 1;    END LOOP;    -- control resumes here    DBMS_OUTPUT.PUT_LINE ('The sum of integers between 1 '      'and 10 is: 'v_sum); END; 

Execute the script, and then answer the following questions:


What output was printed on the screen?


What is the test condition for this loop?


How many times was the loop executed?


How many times will the loop be executed

  1. if v_counter is not initialized ?

  2. if v_counter is initialized to 0?

  3. if v_counter is initialized to 10?


How will the value of v_sum change based on the initial value of v_counter from the previous question?


What will be the value of v_sum if it is not initialized?


How would you change the script to calculate the sum of the even integers between 1 and 100?

Oracle PL[s]SQL by Example
Oracle PL[s]SQL by Example
ISBN: 3642256902
Year: 2003
Pages: 289 © 2008-2017.
If you may any questions please contact us: