Solved

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

Posted on 2002-07-10
5
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

749 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