# Lab 8.2 Exercise Answers

This section gives you some suggested answers to the questions in Lab 8.2, with discussion related to how those answers resulted. The most important thing to realize is whether your answer works. You should figure out the implications of the answers here and what the effects are from any different answers you may come up with.

#### 8.2.1 Answers

 a) What output was printed on the screen? A1: Answer: Your output should look like the following: ` Current sum is: 1 Current sum is: 3 Current sum is: 6 Current sum is: 10 Current sum is: 15 Current sum is: 21 Current sum is: 28 Current sum is: 36 Current sum is: 45 Current sum is: 55 The sum of integers between 1 and 10 is: 55 PL/SQL procedure successfully completed. `

Every time the loop is run, the value of v_counter is checked in the test condition. While the value of v_counter is less than or equal to 10, the statements inside the body of the loop are executed. In this script, the value of v_sum is calculated and displayed on the screen. Next , the value of v_counter is incremented, and control is passed to the top of the loop. Once the value of v_counter increases to 11, the loop is terminated .

For the first iteration of the loop, the value of v_sum is equal to 1, according to the statement

` `
`  v_sum := v_sum + v_counter  `

After the value of v_sum is calculated, the value of v_counter is incremented by 1. Then, for the second iteration of the loop, the value of v_sum is equal to 3, because 2 is added to the old value of v_sum .

After the loop has terminated, "The sum of integers..." and "Done ..." are displayed on the screen.

 b) What is the test condition for this loop? A2: Answer: The test condition for this loop is v_counter <= 10. c) How many times was the loop executed? A3: Answer: The loop was executed 10 times.

Once the value of v_counter reaches 11, the test condition

` `
`  v_counter <= 10  `

evaluates to FALSE, and the loop is terminated.

As mentioned earlier, the loop counter tracks the number of times the loop is executed. You will notice that in this exercise, the maximum value of v_counter is equal to the number of times the loop is iterated.

 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? A4: Answer: If the value of v_counter is not initialized to some value, the loop will not execute at all.

In order for the loop to execute at least once, the test condition must evaluate to TRUE at least once. If the value of v_counter is only declared and not initialized, it is NULL. It is important to remember that null variables cannot be compared to other variables or values . Therefore, the test condition

` `
`  v_counter <= 10  `

never evaluates to TRUE, and the loop is not executed at all.

If v_counter is initialized to 0, the loop will execute 11 times instead of 10, since the minimum value of v_counter has decreased by 1.

When v_counter is initialized to 0, the range of integers for which the test condition of the loop evaluates to TRUE becomes 0 to 10. The given range of the integers has eleven numbers in it. As a result, the loop will iterate eleven times.

If v_counter is initialized to 10, the loop will execute once.

When the initial value of v_counter is equal to 10, the test condition evaluates to TRUE for the first iteration of the loop. Inside the body of the loop, the value of v_counter is incremented by one. As a result, for the second iteration of the loop, the test condition evaluates to FALSE, since 11 is not less than or equal to 10, and control is passed to the next executable statement after the loop.

 e) How will the value of v_sum change based on the initial value of v_counter from the previous question? A5: Answer: When v_counter is not initialized, the loop is not executed at all. Therefore, the value of v_sum does not change from its initial value; it stays 0. When v_counter is initialized to 0, the loop is executed 11 times. The value of v_sum is calculated 11 times, as well. However, after the loop completes, the value of v_sum is 55, because 0 is added to v_sum during first iteration of the loop. When v_counter is initialized to 10, the loop is executed once. As a result, the value of v_sum is incremented only once by 10. After the loop is complete, the value of v_sum is equal to 10. f) What will be the value of v_sum if it is not initialized? A6: Answer: The value of v_sum will be NULL if is not initialized to some value.

The value of v_sum in the statement

` `
`  v_sum := v_sum + 1  `

will always be equal to NULL, because NULL + 1 is NULL. It was mentioned earlier that NULL variables cannot be compared to other variable or values. Similarly, calculations cannot be performed on null variables.

 g) How would you change the script to calculate the sum of the even integers between 1 and 100? A7: Answer: Your answer should be similar to the following. Changes are shown in bold letters . Notice that the value of v_counter is initialized to 2, and with each iteration of the loop, the value of v_counter is incremented by 2, as well. ` -- ch08_3b.sql, version 2.0 SET SERVEROUTPUT ON DECLARE v_counter BINARY_INTEGER := 2; v_sum NUMBER := 0; BEGIN WHILE v_counter <= 100 LOOP v_sum := v_sum + v_counter; DBMS_OUTPUT.PUT_LINE ('Current sum is: 'v_sum); -- increment loop counter by two v_counter := v_counter + 2; END LOOP; -- control resumes here DBMS_OUTPUT.PUT_LINE ('The sum of even integers between ''1 and 100 is: 'v_sum); END; `

Oracle PL[s]SQL by Example
ISBN: 3642256902
EAN: N/A
Year: 2003
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net