Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views4 pages

2 3 1 Summary

The document provides an overview of PL/SQL control structures, which include conditional, iterative, and sequential controls. It details various types of IF statements and the CASE statement for managing execution flow based on conditions. Key takeaways emphasize the efficiency and flexibility of these control structures in programming.

Uploaded by

AMAN AGARWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

2 3 1 Summary

The document provides an overview of PL/SQL control structures, which include conditional, iterative, and sequential controls. It details various types of IF statements and the CASE statement for managing execution flow based on conditions. Key takeaways emphasize the efficiency and flexibility of these control structures in programming.

Uploaded by

AMAN AGARWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PL/SQL Control Structures Summary

1. Introduction to Control Structures


Control structures manage the flow of execution in PL/SQL:
- Conditional Control: Executes statements based on conditions.
- Iterative Control: Repeats execution using loops.
- Sequential Control: Executes statements in order.

2. Conditional Control in PL/SQL


PL/SQL provides IF statements to control execution.

IF Statement Types:
1. IF-THEN Statement
Executes a block if the condition is true.
Syntax:
IF condition THEN
Statement;
END IF;

2. IF-THEN-ELSE Statement
Executes different blocks based on TRUE or FALSE conditions.
Syntax:
IF condition THEN
Statements when TRUE
ELSE
Statements when FALSE
END IF;

3. IF-THEN-ELSIF Statement
Allows multiple conditions.
Syntax:
IF condition1 THEN
Statements for condition1
ELSIF condition2 THEN
Statements for condition2
END IF;
4. IF-THEN-ELSIF-ELSE Statement
Provides default execution when all conditions fail.
Syntax:
IF condition1 THEN
Statements for condition1
ELSIF condition2 THEN
Statements for condition2
ELSE
Statements if all conditions fail
END IF;

Examples:
Example 1: Checking a Condition
DECLARE
a NUMBER := 200;
BEGIN
IF a < 10 THEN
DBMS_OUTPUT.PUT_LINE('a is less than 10');
ELSE
DBMS_OUTPUT.PUT_LINE('a is not less than 10');
END IF;
END;

Example 2: Check Even or Odd


DECLARE
n1 NUMBER := &num1;
BEGIN
IF MOD(n1,2) = 0 THEN
DBMS_OUTPUT.PUT_LINE ('The number '||n1||' is even');
ELSE
DBMS_OUTPUT.PUT_LINE ('The number '||n1||' is odd');
END IF;
END;

Example 3: Check Positive, Negative, or Zero


DECLARE
num1 NUMBER := &get_num;
BEGIN
IF num1 < 0 THEN
DBMS_OUTPUT.PUT_LINE ('Negative number');
ELSIF num1 = 0 THEN
DBMS_OUTPUT.PUT_LINE ('Equal to zero');
ELSE
DBMS_OUTPUT.PUT_LINE ('Positive number');
END IF;
END;

3. PL/SQL CASE Statement


The CASE statement allows structured multiple conditions.
Syntax:
CASE expression
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END;

Example 1: Grade Classification


DECLARE
grade CHAR(1) := 'C';
BEGIN
CASE grade
WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Distinction');
WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('First Class');
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Second Class');
ELSE DBMS_OUTPUT.PUT_LINE('Failed');
END CASE;
END;

Example 2: Display Day of a Date


DECLARE
t_dt DATE := TO_DATE('&input_date', 'DD-MON-YYYY');
t_day VARCHAR2(1);
BEGIN
t_day := TO_CHAR(t_dt, 'D');
CASE t_day
WHEN '1' THEN DBMS_OUTPUT.PUT_LINE('Sunday');
WHEN '2' THEN DBMS_OUTPUT.PUT_LINE('Monday');
WHEN '3' THEN DBMS_OUTPUT.PUT_LINE('Tuesday');
WHEN '4' THEN DBMS_OUTPUT.PUT_LINE('Wednesday');
WHEN '5' THEN DBMS_OUTPUT.PUT_LINE('Thursday');
WHEN '6' THEN DBMS_OUTPUT.PUT_LINE('Friday');
WHEN '7' THEN DBMS_OUTPUT.PUT_LINE('Saturday');
END CASE;
END;

4. Key Takeaways
- IF Statements allow conditional execution.
- CASE Statements provide structured multi-condition checks.
- PL/SQL Control Structures make programs efficient and flexible.

You might also like