Control Statements
- Control statements are most important in PL/SQL.
- Control Statements are elements in a program that controls the flow of program execution.
- The syntax of control statements is very similar to regular English and is very similar to the choices that we make every day.
- Branching statements are as follows:
- If statement
- If – THEN – ELSE
- Nested IF
- Branching with logical connectivity
- While
- For Loop
IF statement
Syntax
IF condition THEN
Statements;
END IF;
- IF and THEN are reserved words and END IF is a reserved phrase that indicates the end of IF and THEN
- IF and THEN are executed only when a condition is either TRUE or FALSE
Example If the price of the book is less then I will buy the book.
DECLARE
no1 NUMBER :=20;
no2 NUMBER;
BEGIN
IF no1=20 THEN
no2:=20;
DBMS_OUTPUT.PUT_LINE('IF condition is TRUE');
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('Value of no2 is :' || no2);
End If;
END;
Output

IF THEN-ELSE
Syntax
IF condition THEN
Statements;
ELSE
Statements;
END IF;
- There are two groups first is evaluated when the condition evaluates to TRUE
- The next group is evaluated when the condition evaluates to FALSE.
Example
If the ticket for a Hollywood movie is less THEN I will go to a Hollywood movie Else I will go to a Marathi movie.
DECLARE
no1 number:=24;
no2 number:=26;
no3 number;
BEGIN
DBMS_OUTPUT.PUT_LINE(‘first number :’|| no1);
DBMS_OUTPUT.PUT_LINE(‘second number :’|| no2);
DBMS_OUTPUT.PUT_LINE(‘’);
If no1 < no2 THEN
DBMS_OUTPUT.PUT_LINE(‘first number is less’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘second number is less’);
END IF;
END;
Output

Nested IF
- If statements can be nested into one another depending on the programmer's requirements.
- A nested IF is a condition in which an IF follows another IF immediately for every true state of the IF condition.
- Every IF block needs to be properly closed with an END IF.
Syntax
If Condition THEN
If Condition THEN
If Condition THEN
Statement1;
ELSE
Statement2;
END IF;
ELSE
Statement3;
END IF;
ELSE
Statement4;
END IF;



Gowtham RajamanickamPosted Apr 25, 2015, 6:23 AM
good