| | |
Boolean Algebra
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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.
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.
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)Syntax of an If Statement
{ ... }
else if (condition)
{ ... }
else
{ ... }
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;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 (x < 4)
cout << "Hello!\n";
else
cout << "Bye!\n";
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) {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.
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;
}
Last edited by cscgal; Dec 24th, 2004 at 2:46 pm.
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/
...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:
no www
no nonsense
coming soon to a pc near you! :cool:
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..... */
(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:
no www
no nonsense
coming soon to a pc near you! :cool:
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;
(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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
- Lookig for a good book on Boolean Algebra (Geeks' Lounge)
- getting the complement of boolean algebra function (Java)
- K-Maps and boolean algebra (Assembly)
- Boolean algebra help (Computer Science)
- Algorithm to Boolean Math Function (Computer Science)
- Questions about assembly and boolean algebra (Assembly)
- Linear Algebra crisis (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: GAAAH! Memory issue?
- Next Thread: IP address
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







