|
-
Oct 24th, 2013, 07:27 AM
#1
Thread Starter
Member
"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?
-
Oct 24th, 2013, 07:33 AM
#2
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
-
Oct 24th, 2013, 07:41 AM
#3
Thread Starter
Member
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.
-
Oct 24th, 2013, 07:44 AM
#4
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
-
Oct 24th, 2013, 07:56 AM
#5
Thread Starter
Member
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.
-
Oct 28th, 2013, 12:34 AM
#6
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.
-
Oct 28th, 2013, 12:43 AM
#7
Re: "Then Else" together with no code between?
 Originally Posted by Logophobic
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?
-
Oct 28th, 2013, 01:33 AM
#8
Re: "Then Else" together with no code between?
 Originally Posted by Logophobic
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)
-
Oct 28th, 2013, 01:45 AM
#9
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
-
Oct 28th, 2013, 09:46 AM
#10
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
-
Oct 30th, 2013, 06:53 PM
#11
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|