Lab 5.3 Exercises


5.3.1 Use Nested IF Statements

In this exercise, you will use nested IF statements. This script will convert the value of a temperature from one system to another. If the temperature is supplied in Fahrenheit, it will be converted to Celsius, and vice versa.

Create the following PL/SQL script:

 
 -- ch05_4a.sql, version 1.0 SET SERVEROUTPUT ON DECLARE    v_temp_in NUMBER := &sv_temp_in;    v_scale_in CHAR := '&sv_scale_in';    v_temp_out NUMBER;    v_scale_out CHAR; BEGIN    IF v_scale_in != 'C' AND v_scale_in != 'F' THEN       DBMS_OUTPUT.PUT_LINE ('This is not a valid scale');    ELSE       IF v_scale_in = 'C' THEN          v_temp_out := ((9 * v_temp_in) / 5) + 32;          v_scale_out := 'F';       ELSE          v_temp_out := ((v_temp_in  32) * 5) / 9;          v_scale_out := 'C';       END IF;       DBMS_OUTPUT.PUT_LINE ('New scale is: '          v_scale_out);       DBMS_OUTPUT.PUT_LINE ('New temperature is: '          v_temp_out);    END IF; END; 

Execute the script, and then answer the following questions:

a)

What output is printed on the screen if the value of 100 is entered for the temperature, and the letter "C" is entered for the scale?

b)

Try to run this script without providing a value for the temperature. What message will be displayed on the screen? Why?

c)

Try to run this script providing an invalid letter for the temperature scale, for example, letter "V." What message will be displayed on the screen? Why?

d)

Rewrite this script so that if an invalid letter is entered for the scale, v_temp_out is initialized to zero and v_scale_out is initialized to C.




Oracle PL[s]SQL by Example
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