Page 1 of 1

Language Usage Question

Posted: 18 February 2008, 5:25 AM
by victorf
Suppose you have a conditional statement like this

If a = 4 and B < 8 I want to do some stuff!

Should it be written:

Code: Select all

method 1. 
If a = 4 And B < 8 Then

  'some stuff here
  
End If

or

method 2.
If &#40;a = 4&#41; And &#40;b < 8&#41; Then

  'some stuff here
  
End If
This, from SysRef, seems to say either is OK:

"Note that the conditional expression is not required to be enclosed in parentheses. Many programmers are accustomed to other languages where they are required and therefore do so out of habit. Others believe that the parentheses improve the readability and use them for that reason. You’re free to adopt whichever practice suits you."

What happens if you write:

Code: Select all

method 3.
If &#40;a = 4&#41; And b < 8 Then

  'some stuff here
  
End If
This is not an urgent thing with me as I am a long time Pascal programmer and instinctively use method 2.

Any enlightenment will be appreciated.

Vic

Re: Language Usage Question

Posted: 18 February 2008, 5:56 AM
by mikep
If you understand Pascal then you know it has operator precedence rules. These rules together with the semantics of the expression you are trying to write determine where you MUST put parenthesis. Other ones can be added to improve readability providing they do not change the semantic meaning of the expression.

ZBasic also has operator precedence rules which might not be the same as Pascal or C. See section 2.4.1 of the Language Reference.

Posted: 18 February 2008, 9:35 AM
by stevech
maybe my C background, but I overuse parentheses to make it abundantly clear what logic was intended; clear to others and clear to me a year later!

Also to make it unambiguous what operator precedence I want, in an equation, since not all compilers follow my-dear-aunt-sally order, and/or some compilers have bugs in consistently implementing precedence (not ZBasic of course!).

No code size penalty to this,.