# Lab 5.2 Exercises

#### 5.2.1 Use the ELSIF Statement

In this exercise, you will use an ELSIF statement to display a letter grade for a student registered for a specific section of course number 25.

Create the following PL/SQL script:

` `
` -- ch05_3a.sql, version 1.0 SET SERVEROUTPUT ON DECLARE    v_student_id NUMBER := 102;    v_section_id NUMBER := 89;    v_final_grade NUMBER;    v_letter_grade CHAR(1); BEGIN    SELECT final_grade      INTO v_final_grade      FROM enrollment     WHERE student_id = v_student_id       AND section_id = v_section_id;    IF v_final_grade BETWEEN 90 AND 100 THEN       v_letter_grade := 'A';    ELSIF v_final_grade BETWEEN 80 AND 89 THEN       v_letter_grade := 'B';    ELSIF v_final_grade BETWEEN 70 AND 79 THEN       v_letter_grade := 'C';    ELSIF v_final_grade BETWEEN 60 AND 69 THEN       v_letter_grade := 'D';    ELSE       v_letter_grade := 'F';    END IF;    -- control resumes here    DBMS_OUTPUT.PUT_LINE ('Letter grade is: '       v_letter_grade); END; `

Note, that you may need to change the values for the variables v_student_id and v_section_id as you see fit in order to test some of your answers.

Try to answer the following questions first, and then execute the script:

 a) What letter grade will be displayed on the screen: if the value of v_final_grade is equal to 85? if the value of v_final_grade is NULL? if the value of v_final_grade is greater than 100? b) How would you change this script so that a message 'v_final_grade is null' is displayed if v_final_grade is NULL? c) How would you change this script so that student ID and section ID are provided by a user ? d) How would you change the script to define a letter grade without specifying the upper limit of the final grade? In the statement, v_final_grade BETWEEN 90 and 100, number 100 is the upper limit.

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