Reading: Boolean Values and Expressions
This reading introduces Boolean (true/false) values and expressions in C.
Comparison Operators
Many (most?) programs later in this course will require the comparison of values, yielding a true or false conclusion. Such comparisons are called Boolean expressions. Simple comparisons may use these relations:
relation symbol | meaning |
---|---|
< | less than |
<= | less than or equal |
> | greater than |
>= | greater than or equal |
== | equal |
!= | not equal |
Notes:
-
Programmers who may have programmed in other languages may mistakenly write = for ==. In C, the compiler will interpret the equal sign = as an assignment. Thus, the statement i = 3 does not compare i to 3 but rather assigns the value 3 to i.
-
In C, the exclamation point ! means "not", so != means "not equal".
Examples of simple comparisons
The following table illustrates several typical comparisons.
Boolean expression | Expression evaluation |
---|---|
1 < 2 | true |
1 > 2 | false |
1 + 4 <= 2 + 3 | true |
2 * 3 != 6 | false |
6 * 2 == 3 * 4 | true |
Combining Boolean Expressions
Beyond these simple connectives (<, <=, >, >=, ==, !=), compound Boolean expressions may be created with logical and (&&) and logical or (||).
Given Boolean values or expressions A and B,
-
Logical and:
- A && B is true if both A and B are true
- A && B is false if either A or B is false, or if they are both false.
-
Logical or:
- A || B is true if either A or B is true, or if they are both true.
- A || B is false if both A and B are false.
Examples of Compound Boolean Expressions
Boolean Expression | Expression Evaluation |
---|---|
(1 < 2) && (3 < 4) | true |
(1 < 2) || (3 < 4) | true |
(1 < 2) && (3 > 4) | false |
(1 < 2) || (3 > 4) | true |
(1 > 2) && (3 > 4) | false |
(1 > 2) || (3 > 4) | false |
Boolean Values in C
Technically, C has no separate Boolean type. Rather Boolean results are considered integers.
-
In C, a zero value is considered to be false.
-
Any non-zero value is considered to be true.
In practice, the result of a comparison is normally 1 (true) or 0 (false). Thus, 1 < 2 and 4 > 3 both evaluate to 1.
Printing Boolean Values
C printf statement | Value printed |
---|---|
printf ("%d", (1 < 2) && (3 < 4)); | 1 |
printf ("%d", (1 < 2) || (3 < 4)); | 1 |
printf ("%d", (1 < 2) && (3 > 4)); | 0 |
printf ("%d", (1 < 2) || (3 > 4)); | 1 |
printf ("%d", (1 > 2) && (3 > 4)); | 0 |
printf ("%d", (1 > 2) || (3 > 4)); | 0 |
A Common Error
It is easy to misuse the comparison operators. In mathematics, the expression:
-2 < -1 < 0
makes sense and would evaluate to true. However, in C, the
semantics of these expressions is different, and the above expression
in C actually evaluates to false. Specifically, comparison
operators associate left to right and evaluate to Boolean values,
so -2 < -1 < 0
is evaluated as
if parentheses were added to give
(-2 < -1) < 0
. The value returned
from the first comparison is 1 (true) because -2 is less than
-1. Next, 1 is compared to 0. 1 is not less than 0 so the expression
evaluates to false.
To do multiple tests at once, use logical AND (&&), and logical OR (||) operators. For example, the previous example can be correctly expressed in C as:
(-1 > -2) && (-1 < 0)
Acknowledgments
Much of this reading evolved specifically to introduce Boolean data types and expressions. That material was written and edited by Henry M. Walker.
The last section of this reading is derived from material written by David Cowden, Dilan Ustek, and Henry M. Walker.
First part of reading: | ||
created 14 July 2016 by Henry M. Walker revised 15 July 2016 by Henry M. Walker | ||
Last section of reading: | ||
created 29 July 2011 by David Cowden full revision 29 July, 2011 by David Cowden minor editing 10 September 2011 by Henry M. Walker full revision 20 September 2011 by Dilan Ustek modest editing 26 September 2011 by Henry M. Walker moderate editing 3 October 2011 by Henry M. Walker moderate editing 5 October 2011 by Dilan Ustek moderate editing 10 October 2011 by Henry M. Walker |
![]() ![]() | |
Full reading: | ||
combined 14 July 2016 by Henry M. Walker revised 15 July 2016 by Henry M. Walker | ||
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu . |