0% found this document useful (0 votes)
105 views22 pages

PL/SQL - Conditions: IF - THEN Statement

The document discusses different types of conditional statements in PL/SQL: 1. IF-THEN statements execute code if a condition is true. IF-THEN-ELSE statements add an "else" block that executes if the condition is false. 2. IF-THEN-ELSIF statements allow choosing between multiple alternatives using additional ELSIF blocks. 3. CASE statements select code to execute based on a selector expression's value rather than boolean conditions. Searched CASE statements have search conditions instead of a selector. 4. Code blocks can be nested, such as an IF statement within an IF or ELSE block, to deepen conditional logic.

Uploaded by

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

PL/SQL - Conditions: IF - THEN Statement

The document discusses different types of conditional statements in PL/SQL: 1. IF-THEN statements execute code if a condition is true. IF-THEN-ELSE statements add an "else" block that executes if the condition is false. 2. IF-THEN-ELSIF statements allow choosing between multiple alternatives using additional ELSIF blocks. 3. CASE statements select code to execute based on a selector expression's value rather than boolean conditions. Searched CASE statements have search conditions instead of a selector. 4. Code blocks can be nested, such as an IF statement within an IF or ELSE block, to deepen conditional logic.

Uploaded by

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

PL/SQL - Conditions

In this chapter, we will discuss conditions in PL/SQL. Decision-making structures require that the programmer
specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to
be executed if the condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical conditional (i.e., decision making) structure found in most of the
programming languages −

PL/SQL programming language provides following types of decision-making statements.

S.No Statement & Description

IF - THEN statement
The IF statement associates a condition with a sequence of statements enclosed by the
1
keywords THEN and END IF. If the condition is true, the statements get executed and if the
condition is false or NULL then the IF statement does nothing.

IF-THEN-ELSE statement
IF statement adds the keyword ELSE followed by an alternative sequence of statement. If the
2
condition is false or NULL, then only the alternative sequence of statements get executed. It
ensures that either of the sequence of statements is executed.

IF-THEN-ELSIF statement
3
It allows you to choose between several alternatives.

Case statement
Like the IF statement, the CASE statement selects one sequence of statements to execute.
4
However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean
expressions. A selector is an expression whose value is used to select one of several alternatives.

Searched CASE statement


5 The searched CASE statement has no selector, and it's WHEN clauses contain search conditions
that yield Boolean values.
nested IF-THEN-ELSE
6 You can use one IF-THEN or IF-THEN-ELSIF statement inside another IF-THEN or IF-THEN-
ELSIF statement(s).

1. IF - THEN statement
It is the simplest form of the IF control statement, frequently used in decision-making and changing the control flow of
the program execution.

Syntax
Syntax for IF-THEN statement is −
IF condition THEN
S;
END IF;
Where condition is a Boolean or relational condition and S is a simple or compound statement. Following is an
example of the IF-THEN statement −
IF (a <= 20) THEN
c:= c+1;
END IF;
If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be
executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if
statement (after the closing end if) will be executed.

Flow Diagram

Example 1
Let us try an example that will help you understand the concept −
DECLARE
a number(2) := 10;
BEGIN
a:= 10;
-- check the boolean condition using if statement
IF( a < 20 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;

2. IF-THEN-ELSE statement

A sequence of IF-THEN statements can be followed by an optional sequence of ELSE statements, which


execute when the condition is FALSE.

Syntax
Syntax for the IF-THEN-ELSE statement is −
IF condition THEN
S1;
ELSE
S2;
END IF;
Where, S1 and S2 are different sequence of statements. In the IF-THEN-ELSE statements, when the test
condition is TRUE, the statement S1 is executed and S2 is skipped; when the test condition is FALSE, then S1 is
bypassed and statement S2 is executed. For example –

IF color = red THEN


dbms_output.put_line('You have chosen a red car')
ELSE
dbms_output.put_line('Please choose a color for your car');
END IF;
If the Boolean expression condition evaluates to true, then the if-then block of code will be executed otherwise
the else block of code will be executed.

Flow Diagram
Example
Let us try an example that will help you understand the concept −
DECLARE
a number(3) := 100;
BEGIN
-- check the boolean condition using if statement
IF( a < 20 ) THEN
-- if condition is true then print the following
dbms_output.put_line('a is less than 20 ' );
ELSE
dbms_output.put_line('a is not less than 20 ' );
END IF;
dbms_output.put_line('value of a is : ' || a);
END;
When the above code is executed at the SQL prompt, it produces the following result −
a is not less than 20
value of a is : 100

PL/SQL procedure successfully completed.

3. The IF-THEN-ELSIF statement allows you to choose


between several alternatives. An IF-THEN statement can be
followed by an optional ELSIF...ELSE statement. The ELSIF clause lets you add additional conditions.
When using IF-THEN-ELSIF statements there are a few points to keep in mind.
 It's ELSIF, not ELSEIF.
 An IF-THEN statement can have zero or one ELSE's and it must come after any ELSIF's.
 An IF-THEN statement can have zero to many ELSIF's and they must come before the ELSE.
 Once an ELSIF succeeds, none of the remaining ELSIF's or ELSE's will be tested.

Syntax
The syntax of an IF-THEN-ELSIF Statement in PL/SQL programming language is −
IF(boolean_expression 1)THEN
S1; -- Executes when the boolean expression 1 is true
ELSIF( boolean_expression 2) THEN
S2; -- Executes when the boolean expression 2 is true
ELSIF( boolean_expression 3) THEN
S3; -- Executes when the boolean expression 3 is true
ELSE
S4; -- executes when the none of the above condition is true
END IF;

Example
DECLARE
a number(3) := 100;
BEGIN
IF ( a = 10 ) THEN
dbms_output.put_line('Value of a is 10' );
ELSIF ( a = 20 ) THEN
dbms_output.put_line('Value of a is 20' );
ELSIF ( a = 30 ) THEN
dbms_output.put_line('Value of a is 30' );
ELSE
dbms_output.put_line('None of the values is matching');
END IF;
dbms_output.put_line('Exact value of a is: '|| a );
END;
When the above code is executed at the SQL prompt, it produces
the following result −
None of the values is matching
Exact value of a is: 100

PL/SQL procedure successfully completed.


4. Case statement Like the IF statement, the CASE
statement selects one sequence of statements to execute. However, to select the sequence, the CASE statement
uses a selector rather than multiple Boolean expressions. A selector is an expression, the value of which is used
to select one of several alternatives.

Syntax
The syntax for the case statement in PL/SQL is −
CASE selector
WHEN 'value1' THEN S1;
WHEN 'value2' THEN S2;
WHEN 'value3' THEN S3;
...
ELSE Sn; -- default case
END CASE;

Flow Diagram

Example
DECLARE
grade char(1) := 'A';
BEGIN
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Well done');
when 'D' then dbms_output.put_line('You passed');
when 'F' then dbms_output.put_line('Better try again');
else dbms_output.put_line('No such grade');
END CASE;
END;
When the above code is executed at the SQL prompt, it produces the following result −
Excellent

PL/SQL procedure successfully completed.

5. The searched CASE statement has no selector and


the WHEN clauses of the statement contain search conditions that
give Boolean values.

Syntax
The syntax for the searched case statement in PL/SQL is −
CASE
WHEN selector = 'value1' THEN S1;
WHEN selector = 'value2' THEN S2;
WHEN selector = 'value3' THEN S3;
...
ELSE Sn; -- default case
END CASE;

Flow Diagram

Example
DECLARE
grade char(1) := 'B';
BEGIN
case
when grade = 'A' then dbms_output.put_line('Excellent');
when grade = 'B' then dbms_output.put_line('Very good');
when grade = 'C' then dbms_output.put_line('Well done');
when grade = 'D' then dbms_output.put_line('You passed');
when grade = 'F' then dbms_output.put_line('Better try again');
else dbms_output.put_line('No such grade');
end case;
END;
When the above code is executed at the SQL prompt, it produces the following result −
Very good

PL/SQL procedure successfully completed.

6. Nested IF-THEN-ELSE It is always legal in PL/SQL programming to nest the IF-ELSE statements, which


means you can use one IF or ELSE IF statement inside another IF or ELSE IF statement(s).

Syntax
IF( boolean_expression 1)THEN
-- executes when the boolean expression 1 is true
IF(boolean_expression 2) THEN
-- executes when the boolean expression 2 is true
sequence-of-statements;
END IF;
ELSE
-- executes when the boolean expression 1 is not true
else-statements;
END IF;

Example
DECLARE
a number(3) := 100;
b number(3) := 200;
BEGIN
-- check the boolean condition
IF( a = 100 ) THEN
-- if condition is true then check the following
IF( b = 200 ) THEN
-- if condition is true then print the following
dbms_output.put_line('Value of a is 100 and b is 200' );
END IF;
END IF;
dbms_output.put_line('Exact value of a is : ' || a );
dbms_output.put_line('Exact value of b is : ' || b );
END;
When the above code is executed at the SQL prompt, it produces the following result −
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

PL/SQL procedure successfully completed.


CASE Statement in Oracle PL/SQL with Examples
ByRichard PetersonUpdatedMarch 4, 2023

What is CASE Statement?


A CASE statement is similar to IF-THEN-ELSIF statement that selects one alternative based on
the condition from the available options.

 CASE statement uses “selector” rather than a Boolean expression to choose the
sequence.
 The value of the expression in the CASE statement will be treated as a selector.
 The expression could be of any type (arithmetic, variables, etc.)
 Each alternative is assigned with a certain pre-defined value (selector), and the
alternative with selector value that matches the conditional expression value will get
executed.
 Unlike IF-THEN-ELSIF, the CASE statement can also be used in SQL statements.
 ELSE block in CASE statement holds the sequence that needs to be executed when none
of the alternatives got selected.

Syntax:
CASE (expression)
WHEN <valuel> THEN action_blockl;
WHEN <value2> THEN action_block2;
WHEN <value3> THEN action_block3;
ELSE action_block_default;
END CASE;
 In the above syntax, the expression will return a value that could be of any type (variable,
number, etc.).
 Each ‘WHEN’ clause is treated as an alternatives which have <value> and <action_block>.
 The ‘WHEN’ clause which matches the value as that of the expression will be selected,
and the corresponding <action_block> will be executed.
 ‘ELSE’ block is optional which hold the <action_block_default> that needs to be executed
when none of the alternatives match the expression value.
 The ‘END’ marks the end of the CASE statement, and it is a mandatory part of the CASE.

Example 1: Arithmetic Calculation using Case

In this example, we are going to do arithmetic calculation between two numbers 55 and 5.

DECLARE
a NUMBER :=55;
b NUMBER :=5;
arth_operation VARCHAR2(20) :='MULTIPLY’;
BEGIN
dbms_output.put_line(‘Program started.' );
CASE (arth_operation)
WHEN ‘ADD’ THEN dbms_output.put_line(‘Addition of the numbers are: '|| a+b );
WHEN ‘SUBTRACT' THEN dbms_output.put_line(‘Subtraction of the numbers are: '||a-b );
WHEN ‘MULTIPLY' THEN dbms_output.put_line(‘Multiplication of the numbers are: '|| a*b
);
WHEN ‘DIVIDE' THEN dbms_output.put_line(‘Division of the numbers are:'|| a/b);
ELSE dbms_output.put_line(‘No operation action defined. Invalid operation');
END CASE;
dbms_output.put_line(‘Program completed.' );
END;
/
Code Explanation:
 Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ data type and initializing it with value
’55’.
 Code line 3: Declaring the variable ‘b’ as ‘NUMBER’ data type and initializing it with value
‘5.’
 Code line 4: Declaring the variable ‘arth_operation’ as ‘VARCHAR2’ data type of size 20
and initializing it with value ‘MULTIPLY’.
 Code line 6: Printing the statement “Program started”.
 Code line 7: CASE checks the value of the expression. In this case, the value of the variable
‘arth_operation’ is ‘MULTIPLY’. This value will be treated as a selector for this CASE
statement now.
 Code line 10: The WHEN clause with value ‘MULTIPLY’ matches with the selector value,
hence controller will select this action_block and will print the message ‘Multiplication of
the numbers are: 275’.
 Code line13: Marks the end of the CASE statement.
 Code line14: Printing the statement “Program completed”.
Code Output:
Program started.
Multiplication of the numbers are: 275
Program completed.

SEARCHED CASE Statement


The SEARCHED CASE statement is similar to the CASE statement, rather than using the selector
to select the alternative, SEARCHED CASE will directly have the expression defined in the WHEN
clause.

 The first WHEN clause that satisfies the condition will be executed, and the controller will
skip the remaining alternatives.

Syntax:
CASE
WHEN <expression1> THEN action_blockl;
WHEN <expression2> THEN action_block2;
WHEN <expression3> THEN action_block3;
ELSE action_block_default;
END CASE;

 In the above syntax, each WHEN clause has the separate <expression> and
<action_block>.
 The WHEN clause for which the expression returns TRUE will be executed.
 ‘ELSE’ block is optional which hold the <action_block_default> that needs to be executed
when none of the alternatives satisfies.
 The ‘END’ marks the end of the CASE statement and, it is a mandatory part of CASE.

Example 1: Arithmetic Calculation using Searched Case

In this example, we are going to do arithmetic calculation between two numbers 55 and 5.

DECLARE a NUMBER :=55;


b NUMBER :=5;
arth_operation VARCHAR2(20) :='DIVIDE';
BEGIN
dbms_output.put_line(‘Program started.' );
CASE
WHEN arth_operation = 'ADD'
THEN dbms_output.put_line(‘Addition of the numbers are: '||a+b );
WHEN arth_operation = ‘SUBTRACT'
THEN dbms_output.put_line(‘Subtraction of the numbers are: '|| a-b);
WHEN arth_operation = ‘MULTIPLY’
THEN dbms_output.put_line(‘Multiplication of the numbers are: '|| a*b );
WHEN arth_operation = ’DIVIDE'
THEN dbms_output.put_line(‘Division of the numbers are: '|| a/b ):
ELSE dbms_output.put_line(‘No operation action defined. Invalid operation');
END CASE;
dbms_output.put_line(‘Program completed.' );
END;
/
Code Explanation:
 Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ data type and initializing it with value
’55’.
 Code line 3: Declaring the variable ‘b’ as ‘NUMBER’ data type and initializing it with value
‘5’.
 Code line 4: Declaring the variable ‘arth_operation’ as ‘VARCHAR2’ data type of size 20
and initializing it with value ‘DIVIDE.’
 Code line 6: Printing the statement “Program started”.
 Code line 7: SEARCHED CASE statement begins. The code from line8 to line 13 is skipped
as their selector value (ADD, SUBTRACT, MULTIPLY) doesn’t match with the value of
‘arth_operation’.
 Code line 14: The WHEN clause expression “arth_operation = ‘DIVIDE'” satisfied and the
expression returns TRUE.
 Code line 15: Action_block of the WHEN clause will be executed, and message ‘Division of
the numbers are: 11’ will be printed.
 Code line 17: Marks the end of CASE statement.
 Code line 18: Printing the statement “Program completed”.

Code Output:
Program started.
Division of the numbers are: 11
Program completed.
Summary
TYPE DESCRIPTION USAGE

Similar to IF-THEN-ELSIF statement. A ‘SELECTOR’ is used to choose Used to select from several


CASE
the alternatives instead of Boolean expression. alternatives using ‘SELECTOR’

CASE statement with no actual ‘SELECTOR’. Instead, it contains the


SEARCHED Used to choose from more than two
actual condition (which evaluates to TRUE/FALSE) that will select the
CASE alternatives mostly.
alternatives.

Oracle PL/SQL IF THEN ELSE Statement: ELSIF,


NESTED-IF
ByRichard PetersonUpdatedMarch 18, 2023
What are Decision-Making Statements?
Decision making statements are those who will decide the flow-control of SQL statements
based on the conditions. It gives the programmer a better control of preventing a particular
code from executing (diagram 1) or choosing a desired code based on the condition (diagram 2).
Below is the pictorial representation of the “Decision Making Statement”.

Decision Making Statement Diagram


Types of Decision Making Statements:

Oracle provides the following types of decision making statements.

 IF-THEN
 IF-THEN-ELSE
 IF-THEN-ELSIF
 NESTED-IF
 CASE
 SEARCHED CASE
In this tutorial, you will learn-

 Introduction to Decision Making Statements


 IF-THEN Statement
 IF-THEN-ELSE Statement
 IF-THEN-ELSIF Statement
 NESTED-IF Statement

IF-THEN Statement
The IF-THEN statement is mainly used to execute a particular section of codes only when the
condition is satisfied.

The condition should yield Boolean (True/False). It is a basic conditional statement which will
allow the ORACLE to execute/skip a particular piece of code based on the pre-defined
conditions.

Syntax for IF THEN Statements:

IF <condition: returns Boolean>


THEN
-executed only if the condition returns TRUE
<action_block>
END if;

 In the above syntax, keyword ‘IF’ will be followed by a condition which evaluates to
‘TRUE’/’FALSE’.
 The control will execute the <action_block> only if the condition returns <TRUE>.
 In the case of condition evaluates to <FALSE> then, SQL will skip the <action_block>, and
it will start executing the code next to ‘END IF’ block.

Note: Whenever condition evaluated to ‘NULL’, then SQL will treat ‘NULL’ as ‘FALSE’.

Example 1: In this example, we are going to print a message when the number is greater than
100. For that, we will execute the following code

To print a message when a number has value more than 100, we execute the following code.

DECLARE
a NUMBER :=10;
BEGIN
dbms_output.put_line(‘Program started.' );
IF( a > 100 ) THEN
dbms_output.put_line('a is greater than 100');
END IF;
dbms_output.put_line(‘Program completed.');
END;
/
Code Explanation:

 Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ data type and initializing it with value
’10’.
 Code line 4: Printing the statement “Program started”.
 Code line 5: Checking the condition, whether variable ‘a’ is greater than ‘100.’
 Code line 6: If ‘a’ is greater than ‘100’, then “a is greater than 100” will be printed. If ‘a’ is
lesser than or equal to 100, then condition fails, so the above printing statement ignored.
 Code line 8: Printing the statement “Program completed”.

Code Output:

Program started.
Program completed.
Example 2: In this example, we are going to print a message if a given alphabet is present in
English vowels (A, E, I, O, U).

To print a message when the given character is Vowel, we execute the following code.

DECLARE
a CHAR(1) :=’u’;
BEGIN
IF UPPER(a) in ('A’,'E','I','0','U' ) THEN
dbms_output.put_line(‘The character is in English Vowels');
END IF;
END;
/
Code Explanation:

 Code line 2: Declaring the variable ‘a’ as ‘CHAR’ of size ‘1’ data type and initializing it with
value ‘u’.
 Code line 4: Checking the condition, whether variable ‘a’ is present in the list
(‘A’,’E’,’I’,’O’,’U’).
 Value of ‘a’ has been converted to uppercase before comparing to make the comparison
is case-insensitive.
 Code line 5: If ‘a’ is present in the list, then the statement “The character is in English
Vowels” will be printed. If condition failed, then this program will not give any output, as
outside the IF-THEN block we have not issued any printing statement.

Code Output:

The character is in English Vowels

IF-THEN-ELSE Statement
 The IF-THEN-ELSE statement is mainly used to select between two alternatives based on
the condition.
 Below is the syntax representation of IF-THEN-ELSE statement.

Syntax for IF-THEN-ELSE Statements:

IF <condition: returns Boolean>


THEN
-executed only if the condition returns TRUE
<action_blockl>
ELSE
-execute if the condition failed (returns FALSE)
<action_block2>
END if;

 In the above syntax, keyword ‘IF’ will be followed by a condition which evaluates to
‘TRUE’/’FALSE’.
 The control will execute the <action_block1> only if the condition returns <TRUE>.
 In case of condition evaluates to <FALSE> then, SQL will execute <action_block2>.
 In any case, one of the two action blocks will be executed.

Note: Whenever condition evaluates to ‘NULL’, then SQL will treat ‘NULL’ as ‘FALSE’.

Example 1: In this example, we are going to print message whether the given number is odd or
even.

DECLARE
a NUMBER:=11;
BEGIN
dbms_output.put_line (‘Program started');
IF( mod(a,2)=0) THEN
dbms_output.put_line('a is even number' );
ELSE
dbms_output.put_line('a is odd number1);
END IF;
dbms_output.put_line (‘Program completed.’);
END;
/
Code Explanation:

 Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ data type and initializing it with value
’11’.
 Code line 4: Printing the statement “Program started”.
 Code line 5: Checking the condition, whether modulus of variable ‘a’ by ‘2’ is 0.
 Code line 6: If ‘0’, then “a is even number” will be printed.
 Code line 7: If the modulus value is not equal to ‘0’, then the condition returns <FALSE>,
so the message “a is odd number” will be printed.
 Code line10: Printing the statement “Program completed”

Code Output:

Program started.
a is odd number
Program completed.

IF-THEN-ELSIF Statement
 The IF-THEN-ELSIF statement is mainly used where one alternative should be chosen
from a set of alternatives, where each alternative has its own conditions to be satisfied.
 The first conditions that return <TRUE> will be executed, and the remaining conditions
will be skipped.
 The IF-THEN-ELSIF statement may contain ‘ELSE’ block in it. This ‘ELSE’ block will be
executed if none of the conditions is satisfied.

Note: ELSE block is optional in this conditional statement. If there is no ELSE block, and none of
the condition satisfied, then the controller will skip all the action block and start executing the
remaining part of the code.

Syntax for IF-THEN-ELSIF Statements:

IF <conditionl: returns Boolean>


THEN
-executed only if the condition returns TRUE <
action_blockl>
ELSIF <condition2 returns Boolean> <
action_block2>
ELSIF <condition3:returns Boolean> <
action_block3>
ELSE —optional
<action_block_else>
END if;

 In the above syntax, the control will execute the <action_block1> only if the condition1
returns <TRUE>.
 If condition1 is not satisfied, then the controller will check for condition2.
 The controller will exit from the IF-statement in the following two cases.
 When the controller found any condition that returns <TRUE>. In this case, the
corresponding action_block will be executed and the controller will exit this IF-
statement block and will start executing the remaining code.
 When none of the conditions satisfied, the then controller will execute ELSE block if
present, then will exit from the IF-statement.

Note: Whenever condition evaluates to ‘NULL’, then SQL will treat ‘NULL’ as ‘FALSE’.

Example 1: Without ELSE block

In this example, we are going to print the grade based on the given marks without else condition
(mark >= 70 Grade A, mark >=40 and mark<70 Grade B, mark >=35 and mark<40 Grade C).

DECLARE
mark NUMBER :=55;
BEGIN
dbms_output.put_line(‘Program started.’ );
IF( mark >= 70) THEN
dbms_output.put_line(‘Grade A’);
ELSIF(mark >= 40 AND mark < 70) THEN
dbms_output.put_line(‘Grade B');
ELSIF(mark >=35 AND mark < 40) THEN
dbms_output.put_line(‘Grade C’);
END IF;
dbms_output.put_line(‘Program completed.’);
END;
/
Code Explanation:

 Code line 2: Declaring the variable ‘mark’ as ‘NUMBER’ data type and initializing it with
value ’55’.
 Code line 4: Printing the statement “Program started”.
 Code line 5: Checking the condition1, whether ‘mark’ is greater or equal 70.
 Code line 7: Since condition1 failed, then the condition2 ’70>mark>=40′ is checked.
 Code line 8: The condtition2 returns <TRUE>, hence the message ‘Grade B’ will be printed.
 Code line12: Printing the statement “Program completed”.
 In this case, the condition3 ‘mark < 35’ will be skipped, as the controller found one
condition which returns <TRUE> before condition3.

Code Output:

Program started.
Grade B
Program completed.
Example 2: With ELSE block

In this example, we are going to print the grade based on the given marks with else condition
(mark >= 70 Grade A, mark >=40 and mark<70 Grade B, mark >=35 and mark<40 Grade C, else
‘No Grade’).
DECLARE
mark NUMBER :=25;
BEGIN
dbms_output.put_line(‘Program started.’ );
IF( mark >= 70) THEN
dbms_output.put_line(‘Grade A’);
ELSIF(mark >= 40 AND mark < 70) THEN
dbms_output.put_line(‘Grade B');
ELSIF(mark >=35 AND mark < 40) THEN
dbms_output.put_line(‘Grade C);
ELSE
dbms_output.put_line(‘No Grade’);
END IF;
dbms_output.put_line(‘Program completed.' );
END;
/
Code Explanation:
 Code line 2: Declaring the variable ‘mark’ as ‘NUMBER’ data type and initializing it with
value ’25’.
 Code line 4: Printing the statement “Program started”.
 Code line 5: Checking the condition 1, whether ‘mark’ is greater or equal 70.
 Code line 7: Since condition1 failed, then the condition2 ’70>mark>=40′ is checked.
 Code line 8: Since condition2 failed, then the condition3 ’40>mark>=35′ is checked.
 Code line 11: Since all the conditions are failed, control will now check for the presence of
ELSE block, and it will print the message ‘No Grade’ from ELSE block.
 Code line14: Printing the statement “Program completed”.

Code Output:

Program started.
No Grade
Program completed.

NESTED-IF Statement
 The NESTED-IF statement is basically allowed programmers to place one or more ‘IF’
condition inside another ‘IF’ condition’s <action_block> other than normal statements.
 Each ‘IF’ condition should have a separate ‘END IF’ statement which marks the end-of-
scope of that particular <action_block>.
 The ‘IF’ statement will consider the nearest ‘END IF’ statement as an endpoint for that
particular condition.
 The pictorial representation for NESTED-IF is shown below diagram.
IF <conditionl: returns Boolean>
THEN
—executed only if the condition returns TRUE
<action block1 starts>
IF <condition2: returns Boolean>
THEN
<action_block2>
END IF; —END IF corresponds to condition2
<action_blockl ends>
END IF; —END IF corresponds to condition1
Syntax Explanation:
 In the above syntax, the outer IF contains one more IF statement in its action block.
 The condition1 returns <TRUE>, then control will be executing <action_block1> and
checks the condition2.
 If condition2 also returns <TRUE>, then <action_block2> will also be executed.
 In case of condition2 evaluates to <FALSE> then, SQL will skip the <action_block2>.

Here we are going to see an example of Nested If –

Example of Nested- If Statement: Greatest of three number

In this example, we are going to print the greatest of three numbers by using Nested-If
statement. The numbers will be assigned in the declare part, as you can see in the code below,
i.e Number= 10,15 and 20 and the maximum number will be fetched using nested-if statements.

DECLARE
a NUMBER :=10;
b NUMBER :=15;
c NUMBER :=20;
BEGIN
dbms_output.put_line(‘Program started.' );
IF( a > b)THEN
/*Nested-if l */
dbms_output.put_line(’Checking Nested-IF 1');
IF( a > c ) THEN
dbms_output.put_line(‘A is greatest’);
ELSE
dbms_output.put_line(‘C is greatest’);
END IF;
ELSE
/*Nested-if2 */
dbms_output.put_line('Checking Nested-IF 2' );
IF( b > c ) THEN
dbms_output.put_line(’B is greatest' );
ELSE
dbms_output.put_line(’C is greatest' );
END IF;
END IF;
dbms_output.put_line(‘Program completed.’ );
END;
/

Code Explanation:

 Code line 2: Declaring the variable ‘a’ as ‘NUMBER’ data type and initializing it with value
’10’.
 Code line 3: Declaring the variable ‘b’ as ‘NUMBER’ data type and initializing it with value
’15’.
 Code line 4: Declaring the variable ‘c’ as ‘NUMBER’ data type and initializing it with value
’20’.
 Code line 6: Printing the statement “Program started” (line 6).
 Code line 7: Checking the condition1, whether ‘a’ is greater than ‘b’ (line 7).
 Code line 10: If ‘a’ is greater than ‘b, then condition in ‘nested-if 1’ will check if ‘a’ is
greater than ‘c'(line 10).
 Code line 13: If still ‘a’ is greater, then message ‘A is greatest’ will be printed (line 11). Else
if condition2 fails, then ‘C is greatest’ will be printed (line 13).
 Code line 18: In case condition1 returns false, then condition in ‘nested-if 2’ will check if
‘b’ is greater than ‘c'(line 18).
 Code line 21: If ‘b’ is greater than ‘c’ , then message ‘B is greatest’ will be printed (line 19),
else if condition2 fails, then ‘C is greatest’ will be printed (line 21).
 Code line 24: Printing the statement “Program completed” (line 24).

Output of code:

Program started.
Checking Nested-IF 2
C is greatest
Program completed.

Summary
In this chapter, we have learned the different decision-making statements and their syntax and
examples. Below table gives the summary of various conditional statements that we have
discussed.

TYPE DESCRIPTION USAGE

Checks for a Boolean condition, if TRUE code in ‘THEN’ block will be To skip,/execute a particular
IF-THEN
executed. code based on the condition.

IF-THEN- Checks for a Boolean condition, if TRUE code in ‘THEN’ block will be Most appropriate in ‘THIS-OR-
ELSE executed, if false code in ‘ELSE’ block is executed. THAT’ condition.

Checks for a Boolean condition in sequential order. The first block in the
IF-THEN- Used to choose from more
sequence which returns TRUE condition will be executed. If none of the
ELSIF than two alternatives mostly.
conditions in the sequence is TRUE, then code in ‘ELSE’ block is executed.

Allows one or more IF-THEN or IF-THEN-ELSIF statement inside another IF- Mainly used in nested
NESTED-IF
THEN or IF-THEN-ELSIF statement(s). condition situation.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy