
- PL/SQL Tutorial
- PL/SQL - Home
- PL/SQL - Overview
- PL/SQL - Environment
- PL/SQL - Basic Syntax
- PL/SQL - Data Types
- PL/SQL - Variables
- PL/SQL - Constants and Literals
- PL/SQL - Operators
- PL/SQL - Conditions
- PL/SQL - Loops
- PL/SQL - Strings
- PL/SQL - Arrays
- PL/SQL - Procedures
- PL/SQL - Functions
- PL/SQL - Cursors
- PL/SQL - Records
- PL/SQL - Exceptions
- PL/SQL - Triggers
- PL/SQL - Packages
- PL/SQL - Collections
- PL/SQL - Transactions
- PL/SQL - Date & Time
- PL/SQL - DBMS Output
- PL/SQL - Object Oriented
- PL/SQL Useful Resources
- PL/SQL - Questions and Answers
- PL/SQL - Quick Guide
- PL/SQL - Useful Resources
- PL/SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PL/SQL - 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.
plsql_conditional_control.htm
Advertisements