Brian Sherwin's Blog

Moving at the Speed of .Net
posts - 98, comments - 62, trackbacks - 30

My Links

News

Twitter












Tag Cloud

Archives

Links

Practice what you preach

In teaching students programming, invariably we always come to a discussion about order of operations.  Anyway, using parentheses will always affect the order of operations regardless of the standard algebraic rules.  After going through lots of examples, the principle that is pronounced for all to abide by--"Always be explicit about what you want to do by using parentheses, don't leave anything to chance."

Well, not following this principle bit me today.  The process went something like:

((x or y) and z) or ((x or w) and z) or x = 1 and a=1

What we really wanted was:

(((x or y) and z) or ((x or w) and z) or x = 1) and a=1

The parentheses are evaluated first, then the "and" then the "or". The difference in parentheses makes the difference between a couple results and a couple thousand results.  Lesson learned.  Illustration gained.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Thursday, August 31, 2006 6:13 PM |

Feedback

Gravatar

# re: Practice what you preach

Admittedly, it's been a while since I took linear-algebra (which is all about truth tables and the type's of logical expressions you have here)... but wouldn't the following also work:

(((x or y or w) and z) or x = 1) and a=1

It's all about the transitive and commutative properties baby!
10/27/2006 4:45 PM | Steve Harman
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: