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.

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 ?!?!??!?

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..... */

yes, the title of this tutorial is slightly misleading.

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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.