Solved

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

Posted on 2002-07-10
5
155 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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now