Language Usage Question

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
victorf
Posts: 342
Joined: 01 January 2006, 4:08 AM
Location: Schenectady, New York

Language Usage Question

Post 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
Vic Fraenckel
KC2GUI
windswaytoo ATSIGN gmail DOT com
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Language Usage Question

Post 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.
Mike Perks
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post 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,.
Post Reply