Conditional Statements

Conditional Statements

Syntax

IF (condition) THEN
   Statement 1;
   Statemnet 2;
        .
  .
   Statement n:
ELSE
   Condition 1;
   Condition 2;
        .
  .
   Condition n;
END IF;
  • This is the conditional statement in the PL/SQL. Which checks the condition either  true or false by means of relational operator such as >, <, >=, <= etc.
  • If the condition is satisfied it executes the statement between if and else. If the condition is not satisfied then it will execute the statements after else.

Note

Every if condition should ends with end if statement.

Write a PL/SQL block input two numbers and find biggest one.

declare
   a number:=&a;
   b number:=&b;
begin
   if(a>b) then
      dbms_output.put_line('a is big');
   elsif(a=b) then
      dbms_output.put_line('both are same');
   else
      dbms_output.put_line('b is big');
   end if;
end;

Write a PL/SQL block input any positive number and check it out even or odd?

declare
   a number:=&a;
begin
   if(a =0) then
      dbms_output.put_line(a||'is neither even nor odd');
   elsif(a MOD 2 =0) then
      dbms_output.put_line(a||'is even');
   else
      dbms_output.put_line(a||'is odd');
   end if;
end;
Conditional Statements
Scroll to top