Home » SQL & PL/SQL » SQL & PL/SQL » if else syntax
if else syntax [message #337905] Fri, 01 August 2008 09:25 Go to next message
shoaib123
Messages: 118
Registered: December 2007
Location: Chicago
Senior Member

Is there any difference between these 2 syntax's..


else if and elsif


Re: if else syntax [message #337907 is a reply to message #337905] Fri, 01 August 2008 09:32 Go to previous messageGo to next message
flyboy
Messages: 1832
Registered: November 2006
Senior Member
Yes, there is a difference between them.
What forces you not to try both and examine the differences?
Re: if else syntax [message #337915 is a reply to message #337905] Fri, 01 August 2008 10:02 Go to previous messageGo to next message
Michel Cadot
Messages: 64102
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
A useful link: PL/SQL User's Guide and Reference

Regards
Michel
Re: if else syntax [message #337919 is a reply to message #337907] Fri, 01 August 2008 10:07 Go to previous messageGo to next message
shoaib123
Messages: 118
Registered: December 2007
Location: Chicago
Senior Member

I don't see any difference, except using "end" at the end of statement.

begin
 if :new.col4 = 6
 then
 :new.col4 := 60;
 else if
  :new.col4 = 7
 then :new.col4 := 70;
 else 
  :new.col4 := 80;
  end if;
 end if;
 end;

begin
 if :new.col4 = 6
 then
 :new.col4 := 60;
 elsif
  :new.col4 = 7
 then :new.col4 := 70;
 else 
  :new.col4 := 80;
  end if;
 end;


Please validate this if i m right..

Appreciate your time on this..
Re: if else syntax [message #337922 is a reply to message #337905] Fri, 01 August 2008 10:17 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
Ready, Fire, AIM!
Re: if else syntax [message #337923 is a reply to message #337919] Fri, 01 August 2008 10:18 Go to previous messageGo to next message
Michel Cadot
Messages: 64102
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I don't see any difference, except

I love this: there is no difference but the differences.

Regards
Michel
Re: if else syntax [message #337925 is a reply to message #337905] Fri, 01 August 2008 10:21 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
When all else fails, RTFM
http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14261/controlstructures.htm#sthref891
Try it & report back what you have learned.
They are different logical constructs which behave differently.
Re: if else syntax [message #337926 is a reply to message #337925] Fri, 01 August 2008 10:27 Go to previous messageGo to next message
shoaib123
Messages: 118
Registered: December 2007
Location: Chicago
Senior Member

Well i re-phrase my words.. I thing there is not any bg difference...

Can somebody validate this ... Smile
Mike has very good sense of humour.. it made me Laughing on myself.

[Updated on: Fri, 01 August 2008 10:28]

Report message to a moderator

Re: if else syntax [message #337927 is a reply to message #337905] Fri, 01 August 2008 10:28 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
When all else fails, RTFM
http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14261/controlstructures.htm#sthref891
Try it & report back what you have learned.
They are different logical constructs which behave differently.
Re: if else syntax [message #337939 is a reply to message #337927] Fri, 01 August 2008 11:54 Go to previous messageGo to next message
shoaib123
Messages: 118
Registered: December 2007
Location: Chicago
Senior Member

If any of the "if" condition fails it jump to the next one.That is exactly happening in both situations using elsif and else if..

What could be the difference.. Is anybody can throw little clue plz..
Re: if else syntax [message #337942 is a reply to message #337905] Fri, 01 August 2008 12:03 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
ELSE IF is logical AND for nested IFs
ELSIF is logical OR for nested IFs

[Updated on: Fri, 01 August 2008 12:27] by Moderator

Report message to a moderator

Re: if else syntax [message #337959 is a reply to message #337942] Fri, 01 August 2008 13:44 Go to previous messageGo to next message
shoaib123
Messages: 118
Registered: December 2007
Location: Chicago
Senior Member

>>> ELSE IF is logical AND for nested IFs


IF sales > (quota + 200) THEN
     bonus := (sales - quota)/4;
  ELSE
     IF sales > quota THEN
        bonus := 50; 
      end if;
       end if;


In this situation,(Please correct me, if i m wrong) it means if the first "if" condition executes as true then it will execute the next step only. If the first "if" condition execute as null or false it will go to "end if".

>> ELSIF is logical OR for nested IFs

BEGIN
  IF grade = 'A' THEN
    DBMS_OUTPUT.PUT_LINE('Excellent');
  ELSIF grade = 'B' THEN
    DBMS_OUTPUT.PUT_LINE('Very Good');
  ELSIF grade = 'C' THEN
    DBMS_OUTPUT.PUT_LINE('Good');
end if;



In this situation(please correct me if i am wrong), it means if the first "if" condition execute as false, then it will go to next "if" condition,

I appreciate your time and help to validate this...




Re: if else syntax [message #337977 is a reply to message #337919] Fri, 01 August 2008 21:15 Go to previous message
flyboy
Messages: 1832
Registered: November 2006
Senior Member
shoaib123 wrote on Fri, 01 August 2008 17:07
Please validate this if i m right..
Validate what? That they return the same result?
They do, but if you had any doubt you would surely run it to prove this; so there must be more something beyound this question.

By the way, the formatting of the first codes is quite strange; I wonder whether it is transparent for you (you can easily find the desired information and accordingly modify it).
Maybe you will see the ELSIF advantage if you use PL/SQL Formatter:
BEGIN
  IF :new.col4 = 6 THEN
    :new.col4 := 60;
  ELSE
    IF :new.col4 = 7 THEN
      :new.col4 := 70;
    ELSE
      :new.col4 := 80;
    END IF;
  END IF;
END;

BEGIN
  IF :new.col4 = 6 THEN
    :new.col4 := 60;
  ELSIF :new.col4 = 7 THEN
    :new.col4 := 70;
  ELSE
    :new.col4 := 80;
  END IF;
END;

Previous Topic: order by letters then numbers
Next Topic: How to get a particular column
Goto Forum:
  


Current Time: Fri Dec 02 20:38:11 CST 2016

Total time taken to generate the page: 0.10551 seconds