IF-THEN-ELSE-END IF Statement.
0
IF..THEN…ELSE…END IF Statement:
Syntax:
IF condition THEN
Sequences of statements;
ELSE
Sequences of statements;
END IF;
Example-1:
SQL>DECLARE
Firstnum NUMBER:=&Fnum;
Secondnum NUMBER:=&Snum;
BEGIN
IF Firstnum<Secondnum THEN
DBMS_OUTPUT.PUT_LINE('Smallest of two number is'||Firstnum);
ELSE
DBMS_OUTPUT.PUT_LINE('Smallest of two number is'||Secondnum);
END IF;
END;
Example-2:
SQL>DECLARE
Num NUMBER:=&Enternumber;
BEGIN
IF MOD(Num,2)=0 THEN
DBMS_OUTPUT.PUT_LINE(Num||' is an Even Number.');
ELSE
DBMS_OUTPUT.PUT_LINE(Num||'is an Odd Number.');
END IF;
END;
Example-3:
SQL>DECLARE
Number1 NUMBER:=& Number1;
Number2 NUMBER:=& Number2;
BEGIN
IF Number1>Number2 THEN
DBMS_OUTPUT.PUT_LINE('The Greatest Number is:'||Number1);
ELSE
IF Number2>Number1 THEN
DBMS_OUTPUT.PUT_LINE('The Greatest Number is:'||Number2);
ELSE
DBMS_OUTPUT.PUT_LINE('The Number are equal'||Number1||'and'||Number2);
END IF;
END IF;
END;
- IF…THEN…ELSE statement enable us to specify two different groups of statements for execution.
- One group is evaluated when the condition evaluates to “FALSE”.
Syntax:
IF condition THEN
Sequences of statements;
ELSE
Sequences of statements;
END IF;
Example-1:
SQL>DECLARE
Firstnum NUMBER:=&Fnum;
Secondnum NUMBER:=&Snum;
BEGIN
IF Firstnum<Secondnum THEN
DBMS_OUTPUT.PUT_LINE('Smallest of two number is'||Firstnum);
ELSE
DBMS_OUTPUT.PUT_LINE('Smallest of two number is'||Secondnum);
END IF;
END;
Example-2:
SQL>DECLARE
Num NUMBER:=&Enternumber;
BEGIN
IF MOD(Num,2)=0 THEN
DBMS_OUTPUT.PUT_LINE(Num||' is an Even Number.');
ELSE
DBMS_OUTPUT.PUT_LINE(Num||'is an Odd Number.');
END IF;
END;
Example-3:
SQL>DECLARE
Number1 NUMBER:=& Number1;
Number2 NUMBER:=& Number2;
BEGIN
IF Number1>Number2 THEN
DBMS_OUTPUT.PUT_LINE('The Greatest Number is:'||Number1);
ELSE
IF Number2>Number1 THEN
DBMS_OUTPUT.PUT_LINE('The Greatest Number is:'||Number2);
ELSE
DBMS_OUTPUT.PUT_LINE('The Number are equal'||Number1||'and'||Number2);
END IF;
END IF;
END;
0 comments: