EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial IF THEN ELSE in Oracle
 

IF THEN ELSE in Oracle

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

IF THEN ELSE in Oracle

 

 

Introduction to IF THEN ELSE in Oracle

In Oracle IF THEN ELSE is a branching statement. This statement enables us to specify two different groups of statements for execution. This statement accepts Boolean expression as a condition, depending on the value of that expression IF THEN ELSE STATEMENT executes or skips a sequence of statements.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

IF Condition
THEN
Statement 1;
ELSE
Statement 2;
END IF;

Explanation:

  • IF: It is a reserved word and marks the beginning of the IF statement
  • Condition: This is a Boolean expression that returns a Boolean result.
  • Statement 1: This is a statement which executes if the condition evaluates the TRUE result
  • ELSE: It executes if the condition returns the FALSE result. It is an option which enables statement.
  • Statement 2:This is a statement that executes if the condition returns a result.
  • ENDIF: This keyword closes or terminates the IF THEN ELSE block or statement

Flowchart of IF THEN ELSE in Oracle

Flowchart of IF … THEN … ELSE Block

Flow chart of IF THEN ELSE Block

Explanation: Both flowcharts clearly show that how IF THEN ELSE statement works. IF block accepts the condition and evaluates to TRUE / FALSE. If the condition returns TRUE then go to the “THEN” Block and executes the “STATEMENT 1” and then execute END block. If the condition returns FALSE then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block. If the condition evaluates to ‘NULL’ then ‘NULL’ will be treated as ‘FALSE’ then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block.

General Points to Ponder About It

Before implementing of Oracle IF THEN ELSE statement, must know some important points about IF THEN ELSE construct:

  • “IF … THEN … ELSE” is a reserved word and marks the beginning of the “IF” statement.
  • This enables us to specify two different groups of statements for execution.
  • One group is evaluated when the condition evaluates to “TRUE”, the next group is evaluated when the condition evaluates to “FALSE”.
  • The “END IF” is a reserved word that indicates the end of the “IF … THEN … ELSE” construct.
  • When “IF … THEN” is executed, a condition is evaluated to either “TRUE” or “FALSE”.
  • The condition need not be specified in brackets.
  • Condition(s) is / are compared by using either the comparison operator(s) or SQL * PLUS operator(s).
  • One “IF” keyword can manage any number of conditions at a point of time using the Logical Connectivity like “AND”, “OR”.
  • Even though the multiple conditions need not be constrained by using brackets, it is better to control their precedence using the proper implementation of brackets to avoid ambiguity.
  • Every “IF” that is implemented needs compulsorily a “TRUE” state evaluation for its successful execution. But an evaluation state is not mandatory when the condition is “FALSE”.
  • After the actual job of “IF” has been completed, the control structure returns the job to the original state of PL / SQL block.

Example to Implement IF THEN ELSE in Oracle

In this section, we’ll see the implementation of Oracle IF … THEN … else how this control structure works in Oracle using some examples. For that, we will use the below sample table (Employee & Dept_category) if required with 14 & 8 records to understand the Oracle IF … THEN … ELSE behavior.

Code:

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

Output:

IF THEN ELSE in Oracle

Explanation:

&EnterNumber: Here “&” enables a user to enter a value within the range and data type criteria.

  • MOD (m, n): It is a predefined function that returns the remainder of being’ divided by ‘n’.
  • DBMS_OUTPUT: DBMS_OUTPUT is an Oracle inbuilt package that allows us to write data to direct output to a screen. This package is used for displaying debugging information.
  • PUT_LINE: It is a procedure that displays the information or results in a line

If MOD function returns 0 then equality condition evaluates “TRUE”  and executes the THEN block statement else executes ELSE block.

As the output showing “ PL/SQL procedure completed” means no error in the above code. But the output is not throwing an expected result. WHY?

It not showing an expected result because there is no setting up control to display output. To set the control, need to execute once “ SET SERVER OUTPUT  ON” in the current user session and it allows the buffer to display the output. See the output for the same example after running the  “ SET SERVER OUTPUT  ON” in the session.

NULL effect

In this output, we got the expected output “10 is an even number”.

The NULL effect in the above PL/SQL Program

NULL effect

In the above output entered value is NULL and in that scenario IF … THEN … ELSE executes the FALSE (ELSE) block that’s why output shows NULL in the place of V_num.

Tips: Whenever a conditional process is executed it is better to cross verify the NULL status of any variable or value before execution. To run any PL/ SQL program use forward slash “/” key. NULL must be handled carefully, NULL can be handled using nested IF or using some other condition.

Conclusion

Oracle IF … THEN … ELSE is a control structure that can be very useful when trying to choose between two mutually exclusive actions. It is used in the PL/SQL block which enables a programmer to handle the real-time requirement in a proper way. This structure operates on Boolean expression and Boolean result.

Recommended Articles

This is a guide to IF THEN ELSE in Oracle. Here we discuss an introduction to IF THEN ELSE in Oracle with syntax, flowchart, and example. You can also go through our other related articles to learn more –

  1. Oracle Operators
  2. Cursor in Oracle
  3. Oracle Queries
  4. Oracle vs MSSQL

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW