Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

10 Experts available now in Live!

Get 1:1 Help Now