Boolean Logic and C++ Conditions

C++ Taking the Bull by the Horns - Part 8

Chrys Forcha
Introduction
This is part 8 of my series, C++ Taking the Bull by the Horns. In this part of the series we apply Boolean logic to C++ conditions. For this part of the series, we assume that a tall man has a height of 20dm and a short man has a height of 10dm. I have exaggerated the numbers for emphasis.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Single Expression Example
Consider the following code:

#include
using namespace std;

int main()
{
//tall means 20
int me = 20;

if (me == 20)
{
cout }

return 0;
}

Read and try the code. In the condition, (parentheses of if) there is only one expression, which is, (me == 20). If this expression results in true, the if-block will be executed. In C++ the number 1 means true in a condition and the number 0 means false. In other words, 1 is the _Bool value for true and 0 is the _Bool value for false. The above if-construct is equivalent to

if (1)
{
cout }

For this second if-construct to be executed, you do not need the creation of the identifier and its assignment. Read and try the following code:

#include
using namespace std;

int main()
{
if (1)
{
cout }

return 0;
}

Let us look at a case where the condition results in false. Consider the following code:

#include
using namespace std;

int main()
{
//short means 10
int me = 10;

if (me == 20)
{
cout }

return 0;
}

The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the identifier, me, is 10 for "short" and not 20 for "tall". The above if-construct is equivalent to:

if (0)
{
cout }

An if-block can only be executed if the condition is true. In this last case it is not executed, since zero means false.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

#include
using namespace std;

int main()
{
if ((0)&&(1))
{
cout }

return 0;
}

A practical example for the above code is:

#include
using namespace std;

int main()
{
//Tall means 20 and short means 10
int you = 20;
int me = 20;

if ((you == 10)&&(me == 20))
{
cout }

return 0;
}

20 is assigned to the identifier, you, and also to the identifier, me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

#include
using namespace std;

int main()
{
if ((0)||(1))
{
cout }

return 0;
}

A practical example for the above code is:

#include
using namespace std;

int main()
{
//Tall means 20 and short means 10
int you = 20;
int me = 20;

if ((you == 10)||(me == 20))
{
cout }

return 0;
}

Read the above code. Try it. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

#include
using namespace std;

int main()
{
if (!(0))
{
cout }

return 0;
}

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

#include
using namespace std;

int main()
{
//Tall means 20 and short means 10
int me = 20;

if (!(me == 10))
{
cout }

return 0;
}

else if and else
You can still add the else-if and else sub statements to the above code samples, following what we learned in one of the previous parts of the series.

A tall man in your area may actually have the height of 1.8m and not 2m as given above. Also a short man in your area may actually have a height of 1.4m and not 1m as given above. You may now ask the question, why did I not use 1.8 for tall and 1.4 for short and then use the float type instead of the int type for the above objects (identifiers). C++ does not handle the float object type in a straightforward way (see later).

Let us stop here and continue in the next part of the series.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):

C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.