C++ part 2 – Boolean operations

(UPDATED 4.5.2013)

In this post I will slightly write about some boolean operations you can do in C++ programming language. The term boolean was named after George Boole (1815–1864) who was “an English mathematician and a founder of the algebraic tradition in logic”. As far as I know, boolean operations are used in many fields from electronics to programming these days so you might hear boolean in another context later also. Anyway we have many boolean operators in C++.  Below is a table of the operators with examples. Note that I have “pseudocode” in the examples so they wont work in C++ compiler.

Syntax  Example  Outcome  Meaning
<         If 1 < 2          True       If 1 is smaller than 2, do something
>         If 1 > 2          False
>=       If 2 >= 1       True       If 2 is bigger(equal) than 1, do something
>=       If 2 >= 2       True
<=       If 2 <= 2       True       If 2 is smaller(equal) than 2, do something
<=       If 2 <= 3       False
!=        If 2 != 1        True       If 2 is NOT 1, do something
!=        If 2 != 2        False
!=        If 2 != 3        True
==       If 2 == 2       True       If 2 equals 2, do something
==       If 2 == 1       False
||          i==2 || i==1  Depends

—————————————————

More operators and theory

Operators    Meaning with the operator “name”
!=               1 NOT 2 then do something
==               2 == 3
||                  OR
&&               If 5 > 3 AND 5 < 7, in programming if i > 0 && i < 5, do something
<                  i < b
>                  i > b

—————————————————

More…

Operator  Meaning (If the inputted or the value that we want to check is on the left side)
<          If the inputted value is smaller than what we want (or dont want) do something
>          If the inputted value is bigger than what we want (or dont want) do something
>=        –..– Bigger or Equals to
<=        –..– Smaller or Equals to
!=         Does not equal
==        Equals
||          OR for example if i == 2 || i == 3 then do something
&&       AND for example if i < 5 AND i > 0 then do something

—————————————————

Below is code with some boolean operations with a picture of the output.

#include <iostream>
using namespace std;
int main()
{

int i = 4;
int u = 2;
int v = 6;

cout << “Value 1 means true and 0 means untrue/false” << endl;
cout << “Is value i bigger than u? “<< ((i > u) && (i < v)) <<endl;
cout << “Is value u smaller than i? “<< (u < i) <<endl;
cout << “Is value v bigger than i AND bigger than u? “<<((v > i) && (v > u)) <<endl;
cout << “Is value i bigger than u AND smaller than v? “<< ((i>u) && (i<v)) <<endl;
cout << ((76573-541)>=4516) << endl;
cout << ((1<0) || (1>0)) << endl;
cout << ( 10 > 100 ) << endl;
cout << ( 40 < 30 ) << endl;
cout << ( 10 <= 10) << endl;
cout << ( 15 >= 10 && 15 <= 20) << endl;
cout << ( 15 != 15) << endl;
cout << ( 15 != 14) << endl;

biggersmaller
system(“PAUSE”);
return 0;
}

—————————————————

#include <iostream>
using namespace std;
int main()
{

int a;
int b;
int compare;
int sum;

cout << “Enter the first whole number: “;
cin >> a;

cout << “Enter the second whole number: “;
cin >> b;

cout << “Enter the whole number you want to compare with: “;
cin >> compare;

sum = a + b;

cout << “The sum of the two inputted whole numbers were: ” << sum << endl;
cout << “The whole number you wanted to compare it with was ” << compare << endl;
cout << “The output of this boolean operation is: ” << (sum > compare) << endl;

system(“PAUSE”);
return 0;
}

—————————————————

In the later tutorials you might also come in contact with the ! syntax where the syntax for example means that if you input a value in a variable a and then somewhere in the code you might have (Note that it’s pseudo code)

!a.equals(1)

Where we check that if a DOES NOT equal 1, do something (Or do not do) depending on your program logic.

—————————————————

By now you should have realized that programming has a lot of mathematical steps to solve a problem i.e. it is called a computational problem solving.

To be able to solve problems in programming, you do not need to have a good mathematical skills, except if you are creating a program to do mathematics.

The problem solving in programs usually contain simple arithmetic operations and algorithmic design to approach every ten line of code.

To be able to understand why, it is done this way, all you need to do is to look at the history of computers. Computers originally were created to be large “calculators”. Even the computer hardware is built around set of mathematical operations.

The problems you will work on, will be mechanical and to be able to solve any kind of problem you must create code so the computer knows what to do.

Right now you cannot see any real life benefits but later you will see more tutorials and syntax where you can actually achieve something. Right now you just need to play with the boolean operations to understand the logic behind them.

As a side note, there is bunch of more boolean operators that we did not cover in this post. You can Google more booleans operators for testing.

References:

http://www.cplusplus.com/doc/tutorial/operators/

http://ocw.mit.edu/index.htm

http://plato.stanford.edu/entries/boole/

One thought on “C++ part 2 – Boolean operations

  1. Pingback: PHP programming – part 1 | Introduction to PHP-programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s