Re: Boolean algebra for beginner

Posted by WBahn on
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/Boolean-algebra-for-beginner-tp4034130p4034145.html

King Julian wrote
interesting comment .. actually Many high level programming languages or rather levels of programming don’t really require Boolean algebra to write programs. My background for example is mainly business application programming levels etc and in reality these levels of Boolean algebra is hardly required in that world. I’ve even done assembler at the business application levels.
I think you'll find it was required and that you did use it, you just didn't realize it for what it was. If you've every used an if() statement or a for(), do(), or while() loop, then you've used boolean expressions. A boolean expression is simply a mathematical statement that produces a boolean result, meaning a result that is either true or false.

For instance, in

if (x < y)
   x = 3 * y;

the expression x < y, is a boolean expression. More specifically, it is a relational expression, which is a subset of boolean expressions.

As another example, say you have a variable 'wages' and a category 'status'. You might have to encode something like: If the wages is greater than 80000 and the status is 'married-jointly', then the marginal tax rate is 22%.

You might write code that looks something like this:

if ( (wages > 80000) && (status.equals("married-jointly")) )
   taxRate = 0.22;

This is a logical expression (something) && (something else), which is another subset of boolean expression, and both (something) and (something else) are boolean expressions themselves.

If you've ever taken something like

if ( (5 < x) && (x < 10) )
   // code if x is between 5 and 10
else
   // code if x is outside 5 and 10

and changed it to

if ( (x <= 5) || (10 <= x) )
   // code if x is outside 5 and 10
else
   // code if x is between 5 and 10

Then you have done boolean algebra without realizing it.