Conditional Statements in PL/SQL : Understanding How PL/SQL Makes Decisions Using Simple Logic

Introduction: Why Conditional Statements Matter in PL/SQL

In real life, we constantly make decisions:

  • If it is raining, we carry an umbrella

  • If marks are above passing, we declare success

  • Otherwise, we take a different action

PL/SQL programs work in the same way. They do not execute all statements blindly. Instead, they check conditions and decide what to execute and what to skip.
This decision-making ability is provided by conditional statements.

In PL/SQL, conditional statements allow a program to:

  • Compare values

  • Check logical conditions

  • Execute different blocks of code based on results

Without conditional statements, PL/SQL programs would be static and unintelligent.


What Is a Conditional Statement in PL/SQL?

A conditional statement is a control structure that allows PL/SQL to:

  • Evaluate a condition

  • Decide which block of code should execute

In simple words:

Conditional statements tell PL/SQL “Do this only if the condition is true.”

Conditions are usually formed using:

  • Relational operators (=, <, >, <=, >=, <>)

  • Logical operators (AND, OR, NOT)


Types of Conditional Statements in PL/SQL

PL/SQL provides the following conditional statements:

  1. IF statement

  2. IF–THEN statement

  3. IF–THEN–ELSE statement

  4. Nested IF–THEN–ELSE

  5. ELSIF ladder

  6. CASE statement

    • Simple CASE

    • Searched CASE

Each one is explained deeply and clearly below.


1. IF Statement in PL/SQL (Basic Decision Making)

Concept Explanation

The IF statement checks a condition and executes a block only when the condition is true.
If the condition is false, PL/SQL simply skips the block.

This is the simplest form of decision making.


Syntax

IF condition THEN
statements;
END IF;

Real-Life Example

Situation:
If a person is above 18 years, allow them to vote.

Logic:

  • Condition → age ≥ 18

  • Action → allow voting


PL/SQL Example

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

Important Points

  • IF executes only when condition is true

  • No alternative action exists

  • END IF is mandatory


2. IF–THEN Statement (Single Condition Execution)

Concept Explanation

The IF–THEN statement is used when:

  • You want to check one condition

  • You want to perform one specific action

This is commonly used for validation checks.


Real-Life Example

Situation:
If balance is less than minimum, display warning.


PL/SQL Example

DECLARE
balance NUMBER := 800;
BEGIN
IF balance < 1000 THEN
DBMS_OUTPUT.PUT_LINE('Low balance warning');
END IF;
END;

When to Use IF–THEN

  • Checking eligibility

  • Validating input

  • Displaying warnings


3. IF–THEN–ELSE Statement (Two-Way Decision)

Concept Explanation

The IF–THEN–ELSE statement allows PL/SQL to choose between two alternatives.

  • If condition is true → IF block executes

  • If condition is false → ELSE block executes

This ensures one block always runs.


Syntax

IF condition THEN
statements;
ELSE
statements;
END IF;

Real-Life Example

Situation:
If exam result is pass, display congratulations; otherwise, display retry message.


PL/SQL Example

DECLARE
marks NUMBER := 45;
BEGIN
IF marks >= 50 THEN
DBMS_OUTPUT.PUT_LINE('You passed the exam');
ELSE
DBMS_OUTPUT.PUT_LINE('You failed the exam');
END IF;
END;

Key Understanding

  • Only one block executes

  • ELSE handles the false condition

  • Useful for binary decisions



4. Nested IF–THEN–ELSE (Multiple Levels of Decision)

Concept Explanation

A nested IF means placing one IF statement inside another IF or ELSE block.

This is used when:

  • Decisions depend on previous decisions

  • Conditions are hierarchical


Real-Life Example

Situation:
If student passed:

  • If marks ≥ 75 → distinction

  • Else → pass


PL/SQL Example

DECLARE
marks NUMBER := 78;
BEGIN
IF marks >= 50 THEN
IF marks >= 75 THEN
DBMS_OUTPUT.PUT_LINE('Passed with distinction');
ELSE
DBMS_OUTPUT.PUT_LINE('Passed');
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('Failed');
END IF;
END;

Important Clarification

  • Nested IF increases logical depth

  • Should be used carefully to avoid complexity

  • Indentation improves readability


5. ELSIF Ladder (Multiple Conditions Handling)

Concept Explanation

The ELSIF ladder is used when there are multiple conditions, and only one should execute.

PL/SQL checks conditions:

  • From top to bottom

  • Stops at the first true condition


Syntax

IF condition1 THEN
statements;
ELSIF condition2 THEN
statements;
ELSIF condition3 THEN
statements;
ELSE
statements;
END IF;

Real-Life Example

Situation:
Grade calculation based on marks.


PL/SQL Example

DECLARE
marks NUMBER := 82;
BEGIN
IF marks >= 90 THEN
DBMS_OUTPUT.PUT_LINE('Grade A');
ELSIF marks >= 75 THEN
DBMS_OUTPUT.PUT_LINE('Grade B');
ELSIF marks >= 50 THEN
DBMS_OUTPUT.PUT_LINE('Grade C');
ELSE
DBMS_OUTPUT.PUT_LINE('Fail');
END IF;
END;

Why ELSIF Is Better Than Nested IF

  • Cleaner logic

  • Easier to read

  • Better performance



6. CASE Statement in PL/SQL (Structured Decision Making)

Concept Explanation

The CASE statement is used when:

  • Multiple conditions are based on one expression

  • Code clarity is important

CASE is often more readable than long IF–ELSIF ladders.


6.1 Simple CASE Statement

Syntax

CASE expression
WHEN value1 THEN statements;
WHEN value2 THEN statements;
ELSE statements;
END CASE;

Real-Life Example

Situation:
Menu-based program.


PL/SQL Example

DECLARE
choice NUMBER := 2;
BEGIN
CASE choice
WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('Addition');
WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('Subtraction');
WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('Multiplication');
ELSE DBMS_OUTPUT.PUT_LINE('Invalid choice');
END CASE;
END;

6.2 Searched CASE Statement

Concept Explanation

The searched CASE allows conditions instead of fixed values.


Syntax

CASE
WHEN condition1 THEN statements;
WHEN condition2 THEN statements;
ELSE statements;
END CASE;

Real-Life Example

Salary bonus calculation.


PL/SQL Example

DECLARE
salary NUMBER := 45000;
BEGIN
CASE
WHEN salary >= 60000 THEN DBMS_OUTPUT.PUT_LINE('High bonus');
WHEN salary >= 40000 THEN DBMS_OUTPUT.PUT_LINE('Medium bonus');
ELSE DBMS_OUTPUT.PUT_LINE('Low bonus');
END CASE;
END;

Difference Between IF–ELSIF and CASE

IF–ELSIFCASE
More flexibleMore structured
Can be lengthyCleaner syntax
Logical conditionsExpression-based

Common Mistakes Students Make (Important)

  • Forgetting END IF

  • Using = instead of :=

  • Missing ELSIF spelling

  • Not enabling DBMS_OUTPUT

  • Overusing nested IF


Final Conclusion

Conditional statements are the decision-making backbone of PL/SQL.
They transform a static block into an intelligent program that reacts to data and conditions.

By mastering:

  • IF

  • IF–ELSE

  • ELSIF

  • CASE

You gain the ability to write real-world PL/SQL logic used in:

  • Banking systems

  • Payroll processing

  • Student management

  • Business applications

 

Comments