Results 1 to 11 of 11

Thread: "Then Else" together with no code between?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    "Then Else" together with no code between?

    Hi, I found a bit of syntax that I don't understand.

    For I = LBound(TextArr) To J + 5

    If Left(TextArr(I), 2) = "SB" Then Else TextArr(I) = ""

    Next I

    I've not seen 'Then' and 'Else' together without any code between, I don't know how that works.

    Does it mean that the line 'NextI' should execute if the first 2 chars of TextArr(I) are "SB"? That might explain the lack of code after "Then".
    In which case the Else statement means "If the first 2 chars are not "SB", set the array element's contents to null".

    Have I got this right?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Then Else" together with no code between?

    That is stupid code. I see people do that sort of thing quite a bit because they apparently don't understand logic. They basically do this:
    Code:
    If someExpression = True Then
        'Do nothing
    Else
        'Do something
    End If
    when they should do this:
    Code:
    If someExpression = False Then
        'Do something
    End If
    You simply negate the condition and get rid of the empty block. In your case, that would be:
    Code:
    If Left(TextArr(I), 2) <> "SB" Then
        TextArr(I) = ""
    End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: "Then Else" together with no code between?

    Yes, I thought that too. I wondered if maybe there was some code after the "Then", and it got commented out at some stage and eventually deleted. Not logical otherwise, as far as I can see, as you said.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: "Then Else" together with no code between?

    In which case the Else statement means "If the first 2 chars are not "SB", set the array element's contents to null".
    Don't confuse an empty string with null as they are 2 different things. The code is setting it to an empty string in cases where the If tests false

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    61

    Re: "Then Else" together with no code between?

    Thanks DataMiser, I'll look into that. So it's not deleting an array element, or making it's value null, but it is setting its value as an empty string. I got that now.

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: "Then Else" together with no code between?

    It isn't "stupid code." It is an optimization for speed.

    If A = B Then Else C

    will execute faster than

    If A <> B Then C

    That said, speed of execution usually isn't an issue. Habitual use of the former is poor style as the latter is more intuitive.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: "Then Else" together with no code between?

    Quote Originally Posted by Logophobic View Post
    It isn't "stupid code." It is an optimization for speed.

    If A = B Then Else C

    will execute faster than

    If A <> B Then C

    That said, speed of execution usually isn't an issue. Habitual use of the former is poor style as the latter is more intuitive.
    It is stupid code because, if you need that level of performance optimisation, what are you using VB for in the first place?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: "Then Else" together with no code between?

    Quote Originally Posted by Logophobic View Post
    It is an optimization for speed.
    Whatever optimization was gained by using the = operator over the <> (if there is indeed one) will be negated by the fact that (in VB6, at least) the Else block is always slower than the Then block.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: "Then Else" together with no code between?

    Personally I prefer to use Not with = rather than the <> as it is more readable at least for me, not sure if it would be faster than <> or not
    Code:
    If Not X=Y Then

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: "Then Else" together with no code between?

    DM - exactly... if you're truly going for speed optimization a Not will out perform <>
    It's one of the things I learned from doing ASM a number of years ago... even with a full if...then...else block... if a majority of your time drops into the else... rewrite it to use the inverted logic ... the THEN clause should always be the primary code path ... if you're looking for speed. (goes with Bonnie's comment). And that's probably true for just about any compiled language, it's not unique to VB.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: "Then Else" together with no code between?

    Code:
    78:     i = 1
    004026E2   mov         dword ptr [i],1
    79:     If Not i = 0 Then
    004026E9   cmp         dword ptr [i],0
    004026ED   je          frmMain::cmdCrash_Click+29Ah (004026fa)
    80:       i = i + 1
    004026EF   mov         eax,dword ptr [i]
    004026F2   add         eax,1
    004026F5   jo          $L93+1Fh (00402775)
    004026F7   mov         dword ptr [i],eax
    81:     End If
    82:
    83:     j = 1
    004026FA   mov         dword ptr [j],1
    84:     If j <> 0 Then
    00402701   cmp         dword ptr [j],0
    00402705   je          frmMain::cmdCrash_Click+2B2h (00402712)
    85:       j = j + 1
    00402707   mov         eax,dword ptr [j]
    0040270A   add         eax,1
    0040270D   jo          $L93+1Fh (00402775)
    0040270F   mov         dword ptr [j],eax
    86:     End If
    techgnome: Was curious whether there was a difference. Breaking out of a compiled VB6 program to the VC++ compiler, it looks to me that both the
    use of "NOT" and "<>" gives the same ASM. Whether this is optimized further???

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width