DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: If Statement with Elseif

  1. #1
    Join Date
    Nov 2004

    If Statement with Elseif

    I want to create an if statement like the following

    If x=y then z=1
    Elseif x=a then z=2
    Elseif x=b then z=3
    Elseif x=c then z=4
    Else z=5
    End if

    however I'm getting an error stating "Else without if" and highlighting the first Elseif in the sequence. Their must be a way of keeping the Elseif and then statements on the same line as above, does anyone know how?

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    This is as close as I can get. There has to be a carriage return after the first "Then":
    If x = y Then
        z = 1
    ElseIf x = a Then: z = 2
    ElseIf x = b Then: z = 3
    ElseIf x = c Then: z = 4
    Else: z = 5
    End If
    You might also consider a Select...Case block:
    Select Case x
        Case Is = y: z = 1
        Case Is = a: z = 2
        Case Is = b: z = 3
        Case Is = c: z = 4
        Case Else: z = 5
    End Select
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2004

    if vs Case

    why would the case be perfered over the if, is it faster or somthing ?

  4. #4
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    It is easier to read and understand.

  5. #5
    Join Date
    Mar 2008
    Lancashire, UK
    I think Select Case is also slighty quicker.
    But maybe it depends what you're comparing.

    With Select Case, you only query the value you're comparing once, so if it's a property of an object, it's more efficient. (You don't repeatadly ask the object what a property's value is)

  6. #6
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    I suspect you are correct about the speed, but, given the speed of today's computers, the extra you pick would probably be almost moot.

  7. #7
    Join Date
    Feb 2004
    Sydney, Australia
    As was suggested earlier, readability is the key (along with correct syntax ;-)

    Try :-
    If x = y Then
        z = 1
    ElseIf x = a Then
        z = 2
    ElseIf x = b Then
        z = 3
    ElseIf x = c Then
        z = 4
        z = 5
    End If
    For correct syntax see :-

    The question of whether or not to use Select Case instead sometimes is not even a consideration. OK if each of the 'elseif's is comparing to the same thing (in your case a value of 'z'). However, 'elseif' can be used with different 'evaluations' that you might want to use in a particular order - e.g. if a=b then whatever elseif c=d then whateverelse else somethingelse endif


    P.S. Not sure that speed is really an issue - comparing 5 things? - unless you are looping through 10**n where 'n' is a significant number. If ultimately you are relying on a user at the keyboard or mouse, then ....
    Last edited by gupex; 07-17-2008 at 07:32 AM.

Similar Threads

  1. Having a little code troubles...
    By Wr3ckin_Cr3w in forum .NET
    Replies: 2
    Last Post: 12-22-2007, 07:17 AM
  2. Project Help!
    By guardianhm in forum VB Classic
    Replies: 7
    Last Post: 01-02-2007, 12:10 PM
  3. SQL "GETDATE" Insertion to Statement
    By dmiller23462 in forum VB Classic
    Replies: 4
    Last Post: 05-06-2006, 01:51 AM
  4. Access And SQL date statement
    By Saiful in forum VB Classic
    Replies: 7
    Last Post: 11-24-2000, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center