Solved

What is the Different if the IF..END IF  statement is coded Like...

Posted on 2002-07-10
5
156 Views
Last Modified: 2010-05-02
Whatis the different between the following code in term of performance Gain?


If X = Y Then Msgbox "X=Y" Else Msgbox "X<>Y"

IF X=Y Then
   Msgbox "X=Y"
Else
    Msgbox "X<>Y"
End if

Thank

0
Comment
Question by:ysk9818
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 7143863
Nothing. They will be translated into the same execution code.
The only difference is readability, so I always go for the second way.
CHeers
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7143983
Yes..readability.

Furthermore, you could replace this:

if a=1 then
  msgbox "test1"
  a=0
  b=3.14
else
  msgbox "test2"
  a=a-1
  b=1.2345
end if

with this:

if a=1 then msgbox "test1":a=0:b=3.14 else msgbox "test2":a=a-1:b=1.2345

and it becomes even MORE unreadable.

The one-line IF structure is really there more for backward compatibilty to when program space was precious, and extra lines of code meant less space for your program.

Do yourself and future maintenance programmers a favor and ALWAYS use the multi-line IF, even if it seems like it could fit on one line like this:
  if bDone then Exit for
Instead do this:

  if bDone then
    Exit For
  End if


0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7144003
only one statement can be executed in the IF or ELSE part by using the first syntax. The second syntax allows you to execute more statements.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7144022
>only one statement can be executed

Well, sort of true.  My example above will work even though multiple statements are included.  The key is to separate them with colons (:), but again, this is for backward compatibility and is certainly much less readable than my second version of the same code.

To verify this, create a form with a textbox and command button then add this code:

Private Sub Command1_Click()
  Dim b As single
  Dim a As Integer
  b = 0
  a = Text1.Text
  If a = 1 Then MsgBox "test1": a = 0: b = 3.14 Else MsgBox "test2": a = a - 1: b = 1.2345
  Debug.Print a, b
End Sub

Depending on the value in the textbox, either all of the THEN statements are executed (and values show in the debug window) or all of the ELSE statements are executed.

--
I recommend that if you find code like the above that you convert it to multiline format.
0
 

Author Comment

by:ysk9818
ID: 7144029
Thank for all of your comment, since it is only for readability issue, I am agreering with all of your comments, but i can only give the point to one person, and since angels is the first one responding to me i think it is fair that i will give the credit to angels...thank everybody
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 61
message box in access 4 46
How to measure sizes and angles in scanned images ? 3 63
TT Auto Dashboard 13 92
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question