Experiment No 18: Implement PL/SQL program using Conditional Statements

 Experiment No 18: Implement PL/SQL program using Conditional Statements


Result:
The PL/SQL programs using conditional statements were successfully executed, producing the expected outputs and demonstrating decision-making in PL/SQL.


1. List conditional statements in PL/SQL

In PL/SQL, the following conditional statements are used to control program flow based on conditions:

  1. IF statement – Executes a block of code if a condition is true.

  2. IF-ELSE statement – Executes one block if a condition is true, another if false.

  3. IF-ELSIF-ELSE statement – Checks multiple conditions sequentially and executes the corresponding block.

  4. Nested IF statement – An IF statement inside another IF to handle complex conditions.


2. Describe any three conditional statements in PL/SQL

a) IF Statement

  • Purpose: Executes a block of code only if a given condition is true.

  • Syntax:

IF condition THEN
   statement;
END IF;
  • Example (Indian context – checking student marks):

DECLARE
    marks NUMBER := 85;
BEGIN
    IF marks >= 40 THEN
        DBMS_OUTPUT.PUT_LINE('Student has passed the exam.');
    END IF;
END;

Output:

Student has passed the exam.

b) IF-ELSE Statement

  • Purpose: Executes one block if a condition is true, another if false.

  • Syntax:

IF condition THEN
   statement1;
ELSE
   statement2;
END IF;
  • Example (Indian context – checking election eligibility):

DECLARE
    age NUMBER := 20;
BEGIN
    IF age >= 18 THEN
        DBMS_OUTPUT.PUT_LINE('Eligible to vote in India.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Not eligible to vote.');
    END IF;
END;

Output:

Eligible to vote in India.

c) IF-ELSIF-ELSE Statement

  • Purpose: Checks multiple conditions and executes the block corresponding to the first true condition.

  • Syntax:

IF condition1 THEN
   statement1;
ELSIF condition2 THEN
   statement2;
ELSE
   statement3;
END IF;
  • Example (Indian context – categorizing income group):

DECLARE
    income NUMBER := 60000;
BEGIN
    IF income < 25000 THEN
        DBMS_OUTPUT.PUT_LINE('Low Income Group');
    ELSIF income BETWEEN 25000 AND 100000 THEN
        DBMS_OUTPUT.PUT_LINE('Middle Income Group');
    ELSE
        DBMS_OUTPUT.PUT_LINE('High Income Group');
    END IF;
END;

Output:

Middle Income Group


Exercise


1. Check if a number is positive

DECLARE
    num NUMBER := 25;  -- You can change this value to test other numbers
BEGIN
    IF num > 0 THEN
        DBMS_OUTPUT.PUT_LINE('Number is positive');
    END IF;
END;

Output:

Number is positive

2. Check voting eligibility

DECLARE
    age NUMBER := 20;  -- You can change this value to test other ages
BEGIN
    IF age >= 18 THEN
        DBMS_OUTPUT.PUT_LINE('You can vote');
    ELSE
        DBMS_OUTPUT.PUT_LINE('You cannot vote');
    END IF;
END;

Output (for age = 20):

You can vote

3. Assign grades based on percentage

DECLARE
    percentage NUMBER := 68;  -- Change this value to test different percentages
BEGIN
    IF percentage >= 75 THEN
        DBMS_OUTPUT.PUT_LINE('Distinction');
    ELSIF percentage >= 60 THEN
        DBMS_OUTPUT.PUT_LINE('First Class');
    ELSIF percentage >= 45 THEN
        DBMS_OUTPUT.PUT_LINE('Second Class');
    ELSIF percentage >= 40 THEN
        DBMS_OUTPUT.PUT_LINE('Pass Class');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Fail');
    END IF;
END;

Output (for percentage = 68):

First Class



Popular posts from this blog