Boolean Algebra

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Boolean Algebra

 
0
  #1
Nov 16th, 2003
True and False
The backbone of computer science is logic. Most often, those with logical minds make the best programmers. Hence, a very important programming concept is that of boolean algebra.

A boolean expression is a true or false statement. In the world of computers, a true statement is represented by 1 while a false statement is represented by 0. Low level computer programming (the logic inside a computer) is actually a series of zeros and ones.

The following chart diagrams the most common symbols of equality used in boolean algebra. Note that assignment statements use a single equals such as = while boolean algebra uses the double equals ==.

Symbol Usage
== equality
< less than
> greater than
>= greater than or equal
<= less than or equal
! not
&& and
|| or

For example, the statement cout << (3 == 4); will print a 0 to the screen since it is false that 3 is equal to 4. However, the statement cout << !(3 == 4); will print a 1 since it is true that it is NOT true that 3 is equal to 4. Boolean algebra can become a bit confusing, but with some practice is is extremely logical. It may be wise to investigate truth tables and discrete mathematics when first introduced to boolean algebra.

Branching
Boolean algebra is used to create branch situations in programs. For example, if a statement is true, do one sequence of events. And if a statement is not true, do another sequence. In this way, a program doesn't operate the exact same way step-by-step each time it is run.

This is where If-Else statements come into play. An If-Else statement can be used to allow the computer to perform a different set of actions depending on whether a condition is true or false. If-Else statements follow the following general syntax:


if (condition)


{ ... }
else if (condition)
{ ... }
else
{ ... }



Syntax of an If Statement


When implementing If-Else statements, it is very important that conditions be in parenthesis. Conditions are required following an if and else if and are not allowed for elses. If statements are required while you can have none or multiple else ifs and elses, depending on the particular algorithm you are implementing. Brackets are required to enclose multi-lines although are unnecessary if you only have a single statement being executed following each condition.


int x = 3;


if (x < 4)
cout << "Hello!\n";
else
cout << "Bye!\n";



The above is a very simple segment of code which prints Hello! each time it is executed since the value of x is hard-coded as 3. Therefore, x will always be less than 4, and only Hello! is printed out. The else statement, implying that x is not less than four, is completely overlooked. Note that two sets of brackets enclosing the two cout statements are optional, as only one statement of code is to be executed under each condition. If, for example, ten different operations were to occur if x was less than 4, those ten statements would be enclosed in brackets.



If-Else statements can be nested inside one another. When dealing with them, they are best not taken line-by-line, but rather as a nested hierarchy of components to be taken piece by piece.

Switches
Yet another form of branching can be expressed in the form of a switch statement. In such a case, there are a set number of posibilities. The computer runs down the list from the first down to the last until the condition being tested is met. Once this condition has been "found," directions are executed.

The numbers 1, 2, and 3 after the word case are the values which the computer is testing to see if number is equal to. For example, if number is equal to 1 when the program enters the switch, the computer will print out "the value is one." The break line tells the computer to exit the switch, and to not keep testing to see if another possibility works. Almost every switch includes a default statement, which tells the computer what to do if none of the above cases are true. If the break statements were removed, the computer would always print out the contents of the default statement, regardless of if another case were true.


switch (number) {



case 1:
cout << "the value is one.";
break;

case 2:
cout << "the value is two.";
break;

case 3:
cout << "the value is three.";
break;

default:
cout << "the value isn't 1, 2 or 3";
break;

}



Please note that while the default case is optional, it is good programming practice to use it. Often, it is used as an error-check.
Last edited by cscgal; Dec 24th, 2004 at 2:46 pm.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Boolean Algebra

 
0
  #2
Dec 4th, 2004
nice tut but correct me if i am wrong...

...I thought boolean algebra was a collection of algebraic expressions about logic?

ie

x + (x y) = x
x (x + y) = x
x + (-x) = 1
x (-x) = 0 and the like....

i found a doc here about boolean algebra which is going on about something completely different ?!?!??!?

http://plato.stanford.edu/entries/boolalg-math/
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Boolean Algebra

 
0
  #3
Dec 4th, 2004
btw there is also another expression u can use for a 'choice'

(x == y) ? a : b

/* if its true it returns a, other wise b. I think this is the syntax as i came across it in an old c++ book 2 years ago. my need to have "if" before it..... */
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Boolean Algebra

 
0
  #4
Dec 4th, 2004
yes, the title of this tutorial is slightly misleading.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Boolean Algebra

 
0
  #5
Dec 13th, 2004
Correction:

(statment) ? a : b

if the statement is true it takes a
if the statement is false it takes b

however because you cant use block statements like the if function it is only useful to save coding space if the only action taken is to give the variable a value. for example

if(x == 0)
{
MessageBox(NULL, "X = 0", "Message", MB_ICONINFORMATION | MB_OK);
SomeOtherFunction();
...
}
else
{
....
}

cannot be simplified but...

the abs function (always +) which is:

if(a > 0)
return a;
else
return -a;

can be defined as

#define abs(a) (a > 0) ? a : -a;
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 150
Reputation: lol_hacker101 has a little shameless behaviour in the past 
Solved Threads: 0
lol_hacker101's Avatar
lol_hacker101 lol_hacker101 is offline Offline
Junior Poster

Re: Boolean Algebra

 
0
  #6
Dec 5th, 2006
It's pretty useful, but as you mentioned in the tutorial, this 0 = false and 1= true coding is only used in old computer logics.
"The only thing that expands faster than the fabric of the universe is the fabric of my waistband."

-Albert Einstein
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC