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:
a) |
What output was printed on the screen? | b) |
What is the test condition for this loop? | c) |
How many times was the loop executed? | d) |
How many times will the loop be executed
if v_counter is not initialized? if v_counter is initialized to 0? if v_counter is initialized to 10?
| e) |
How will the value of v_sum change based on the initial value of v_counter from the previous question? | f) |
What will be the value of v_sum if it is not initialized? | g) |
How would you change the script to calculate the sum of the even integers between 1 and 100? |
|
No comments:
Post a Comment