one

Sure! Here's the content formatted as it would appear in a blog:


IF THEN Statement: A Few Examples

1. Checking Age to Determine Eligibility

DECLARE
    age INT := 20;
BEGIN
    IF age >= 18 THEN
        DBMS_OUTPUT.PUT_LINE('Eligible to vote.');
    END IF;
END;
  • Explanation: If the age is greater than or equal to 18, the message "Eligible to vote." is printed.

2. Checking for Even Number

DECLARE
    number INT := 4;
BEGIN
    IF MOD(number, 2) = 0 THEN
        DBMS_OUTPUT.PUT_LINE('The number is even.');
    END IF;
END;
  • Explanation: If the number is even (i.e., the remainder when divided by 2 is 0), the message "The number is even." is printed.

3. Checking if a Student Passed

DECLARE
    score INT := 75;
BEGIN
    IF score >= 50 THEN
        DBMS_OUTPUT.PUT_LINE('You passed the exam!');
    END IF;
END;
  • Explanation: If the student's score is 50 or higher, the message "You passed the exam!" is printed.

4. Checking if a Product is in Stock

DECLARE
    product_stock INT := 0;
BEGIN
    IF product_stock > 0 THEN
        DBMS_OUTPUT.PUT_LINE('Product is in stock.');
    END IF;
END;
  • Explanation: If the product_stock is greater than 0, it prints "Product is in stock." If not, nothing is printed.

5. Validating User Input

DECLARE
    username VARCHAR2(20) := 'admin';
BEGIN
    IF username = 'admin' THEN
        DBMS_OUTPUT.PUT_LINE('Welcome, Admin!');
    END IF;
END;
  • Explanation: If the username is 'admin', it prints "Welcome, Admin!".

These examples show how the IF THEN statement is used to perform different checks and execute code based on conditions.

No comments:

Post a Comment