Boolean Logic and Conditions in C
Foreword: Boolean Logic and Conditions in C
By: Chrysanthus Date Published: 31 May 2024
Introduction
Single Expression Example
Consider the following code:
#include <stdio.h>
int main(int argc, char *argv[])
{
//tall means 20
int me = 20;
if (me == 20)
{
printf("I am tall\n");
}
return 0;
}
All the code of interest is in the C main() function. Read and test 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)
{
printf("I am tall\n");
}
For this second if-construct to be executed, the creation of the identifier and its assignment, are not needed. Read and test the following program:
#include <stdio.h>
int main(int argc, char *argv[])
{
if (1)
{
printf("I am tall\n");
}
return 0;
}
The output is "I am tall", without the quotes. Now, consider the following situation, where the condition results in false:
//short means 10
int me = 10;
if (me == 20)
{
printf("I am tall\n");
}
The if-block (curly brackets) 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)
{
printf("I am tall\n");
}
An if-block can only be executed if the condition is true. In this last situation, it is not executed, since zero means false.
More than One Expression in Condition
There can be more than one expression in a condition. In this tutorial, a maximum of two expressions in a condition, is considered. 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 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
where true or false as an operand, will be replaced with an expression that results in true or false, respectively.
Double-Expression Examples
#include <stdio.h>
int main(int argc, char *argv[])
{
if ((0)&&(1))
{
printf("We are tall\n");
}
return 0;
}
A practical example for the above code is:
#include <stdio.h>
int main(int argc, char *argv[])
{
//Tall means 20 and short means 10
int you = 20;
int me = 20;
if ((you == 10)&&(me == 20))
{
printf("We are tall\n");
}
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 <stdio.h>
int main(int argc, char *argv[])
{
if ((0)||(1))
{
printf("We are tall\n");
}
return 0;
}
A practical example for the above code is:
#include <stdio.h>
int main(int argc, char *argv[])
{
//Tall means 20 and short means 10
int you = 20;
int me = 20;
if ((you == 10)||(me == 20))
{
printf("We are tall\n");
}
return 0;
}
Read the above code. Test 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
#include <stdio.h>
int main(int argc, char *argv[])
{
if (!(0))
{
printf("I am tall\n");
}
return 0;
}
The if-block is executed, if the condition is true. !(false) gives true.
A practical example for the above code is:
#include <stdio.h>
int main(int argc, char *argv[])
{
//Tall means 20 and short means 10
int me = 20;
if (!(me == 10))
{
printf("I am tall\n");
}
return 0;
}
else if and else
The else-if and else sub statements, can still be added to the above code samples, following what has been learned in one of the previous tutorials of this course.
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.