Solved

# paren or not paren

Posted on 2000-03-30
210 Views
Can anyone tell me the difference between:
If (x = 0) Then

and

If x = 0 Then

someone told me that there was a difference. If there is, then when would you use one over the other?
0
• 4
• 3
• 3
• +2

LVL 1

Expert Comment

ID: 2670756
y + x * 8 = 80
y + (x * 8) = 52

0

LVL 1

Expert Comment

ID: 2670760
y + x * 8 = 80
y + (x * 8) = 52

0

LVL 1

Expert Comment

ID: 2670768
y + x * 8 = 80
y + (x * 8) = 52

0

LVL 1

Expert Comment

ID: 2670776
oops sorry for the tripple message
0

LVL 7

Expert Comment

ID: 2670783
The only difference I'm aware of is ease of reading.

There could be less certainty with something like the following:

If X = 0 And Y = 1 Then

and

If (X = 0) And (Y = 1) Then

Without the parenthesis, VB could interpret as

If X = (0 And Y)

and get results you aren't expecting. (I don't honestly know how VB responds in this case. I always use parenthesis in these kinds of cases to avoid ambiguity. But, then again, I'm one of those bastards who always comments my code, too.)

With the simple expression in your example, though, there shouldn't be a functional difference, I don't think.

-- b.r.t.
0

Author Comment

ID: 2670792
Yeah, I know about that. That doesnt answer the question for comparison though. Im using x = 0 and (x = 0) in an if then else statement.
0

LVL 6

Expert Comment

ID: 2670803
There are two reasons for this.

1. if (X=0)=Boolean

For example:
List1.enabled=(x=0)

2. if you take 5+2+7-3+10=?

a.(5+2)+(7-3)+10=21
b. 5+(2+7)-(3+10)=1

The grouping is critical.

Good luck!!

0

Author Comment

ID: 2670821
If (x = 0) Then
....
and

If x = 0 Then
....
What is the difference here?
0

LVL 6

Accepted Solution

DrDelphi earned 50 total points
ID: 2670831
x=0 is a mathematical expression

(x=0) is treated as a codition to either be met or not, hence a boolean. I thought I made that clear last time.

0

LVL 6

Expert Comment

ID: 2670841
also, in this particular case there is no real difference.... but for giggles try this:

x=5

debug.print (x=0)

0

LVL 4

Expert Comment

ID: 2670871
Funny enough debug.print x = 0 also displays False, so it seems that both x = 0 and (x = 0) result in a Boolean value depending of course on the context it's used in.
0

Author Comment

ID: 2670882
Thanks
0

## Featured Post

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â€¦
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
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â€¦