Quantum Computing Beyond the Basics
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
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.
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)
PL/SQL provides the following conditional statements:
IF statement
IF–THEN statement
IF–THEN–ELSE statement
Nested IF–THEN–ELSE
ELSIF ladder
CASE statement
Simple CASE
Searched CASE
Each one is explained deeply and clearly below.
This is the simplest form of decision making.
IF condition THENstatements;END IF;
Logic:
Condition → age ≥ 18
Action → allow voting
DECLAREage NUMBER := 20;BEGINIF age >= 18 THENDBMS_OUTPUT.PUT_LINE('Eligible to vote');END IF;END;
IF executes only when condition is true
No alternative action exists
END IF is mandatory
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.
DECLAREbalance NUMBER := 800;BEGINIF balance < 1000 THENDBMS_OUTPUT.PUT_LINE('Low balance warning');END IF;END;
Checking eligibility
Validating input
Displaying warnings
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.
IF condition THENstatements;ELSEstatements;END IF;
DECLAREmarks NUMBER := 45;BEGINIF marks >= 50 THENDBMS_OUTPUT.PUT_LINE('You passed the exam');ELSEDBMS_OUTPUT.PUT_LINE('You failed the exam');END IF;END;
Only one block executes
ELSE handles the false condition
Useful for binary decisions
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
If marks ≥ 75 → distinction
Else → pass
DECLAREmarks NUMBER := 78;BEGINIF marks >= 50 THENIF marks >= 75 THENDBMS_OUTPUT.PUT_LINE('Passed with distinction');ELSEDBMS_OUTPUT.PUT_LINE('Passed');END IF;ELSEDBMS_OUTPUT.PUT_LINE('Failed');END IF;END;
Nested IF increases logical depth
Should be used carefully to avoid complexity
Indentation improves readability
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
IF condition1 THENstatements;ELSIF condition2 THENstatements;ELSIF condition3 THENstatements;ELSEstatements;END IF;
DECLAREmarks NUMBER := 82;BEGINIF marks >= 90 THENDBMS_OUTPUT.PUT_LINE('Grade A');ELSIF marks >= 75 THENDBMS_OUTPUT.PUT_LINE('Grade B');ELSIF marks >= 50 THENDBMS_OUTPUT.PUT_LINE('Grade C');ELSEDBMS_OUTPUT.PUT_LINE('Fail');END IF;END;
Cleaner logic
Easier to read
Better performance
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.
CASE expressionWHEN value1 THEN statements;WHEN value2 THEN statements;ELSE statements;END CASE;
DECLAREchoice NUMBER := 2;BEGINCASE choiceWHEN 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;
The searched CASE allows conditions instead of fixed values.
CASEWHEN condition1 THEN statements;WHEN condition2 THEN statements;ELSE statements;END CASE;
Salary bonus calculation.
DECLAREsalary NUMBER := 45000;BEGINCASEWHEN 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;
| IF–ELSIF | CASE |
|---|---|
| More flexible | More structured |
| Can be lengthy | Cleaner syntax |
| Logical conditions | Expression-based |
Forgetting END IF
Using = instead of :=
Missing ELSIF spelling
Not enabling DBMS_OUTPUT
Overusing nested IF
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
Post a Comment