Decisions are based on *Boolean expressions*,
algebra-like expressions with a value of true or false.
The term Boolean is named after George Boole,
a well-known mathematician, philosopher, and logician, who
developed a systematic approach to logic now known as
*Boolean algebra*. The basis of Boolean
algebra was published in his text "The Laws of Thought", and
provides a significant part of the foundation of computer science.

The C language does not have a separate Boolean data type.
Recall that the design philosophy of C is minimalist and explicit
rather than bloated and abstract.
Boolean values in C are represented as they are stored internally,
as integer values. A value of 0 is interpreted as `false`

and any non-zero value is interpreted as `true`

.
A derived Boolean type called `bool`

is
provided in the standard header file `stdbool.h`

,
along with `true`

and `false`

constants. We can use this to make our C code more self-documenting,
rather than defining Boolean variables as `int`

.

Since the C language does not have a separate Boolean data type, there are no built-in constants. Traditionally, C programmers would define a Boolean variable as an integer.

#include <sysexits.h> int main() { int raining; raining = 0; // 0 means false, non-zero means true ... return EX_OK; }

However, the `bool`

data type and the values
`true`

and `false`

are
now defined in the standard header file
`stdbool.h`

. While not part of the C
language, it is included with all modern C compilers.

#include <stdbool.h> #include <sysexits.h> int main() { bool raining; raining = false; ... return EX_OK; }

Fortran provides logical constants `.true.`

and `.false.`

. The periods are actually part of
the constants, and are a vestige of
early Fortran, which used periods to delineate many
program elements (tokens) in order to make parsing easier.

program example implicit none logical raining raining = .false. end program

A *relational expression* is a Boolean
expression (an expression with a value of true or false)
that compares (tests the relation between) two values
of any type, such as integers, real numbers, or strings.

Relational expressions in C are actually integers, with
a value of 0 for false and 1 for true, which
can be represented as `false`

and `true`

if `stdbool.h`

is included in the program.
C constructs such as `if`

statements and loops will
interpret a value of zero as false and any non-zero value as true.

Relational expressions in Fortran have the data type
`logical`

and a value
of `.false.`

or `.true.`

.

Relational expressions are formed using
*Relational operators* and two values.
C and Fortran provide the usual arithmetic
relations such as "equals", "less than",etc.

Floating point values should NEVER be compared for equality. Due to round-off error, it is possible that the actual value might be slightly different than the expected value and therefore not equal to the value we're looking for. For example, the following condition is likely to fail, because 0.1 cannot be accurately represented in binary floating point. Even if it could be represented accurately, the value of thickness might be off due to round off during computations.

if ( thickness == 0.1 )

Fortran 90 and later use more intuitive symbols for operators, while Fortran 77 and earlier use old style operators. Fortran 90 is backward-compatible with Fortran 77, so the old style operators can still be used. This allows old Fortran 77 code to be compiled with the newer compilers, but newly written code should use the new operators.

**Table 19.2. Relational Operators**

Relational Operator | C | Fortran 90 and Later | Fortran 77 and Earlier |
---|---|---|---|

Not equal | != | /= | .ne. |

Equals | == | == | .eq. |

Less than | < | < | .lt. |

Less or equal | <= | <= | .le. |

Greater than | > | > | .gt. |

Greater or equal | >= | >= | .ge. |

Fortran 90 adopted the operators from C to replace the arcane .ne., etc., except for !=, because '!' marks the beginning of a comment in Fortran.

Some Boolean expressions entail more than one relation. For example, an airline may want to offer free peanuts to both kids and seniors, in which case we would want to know if age < 18 or age >= 65.

What is the data type of a Boolean expression in C? In Fortran?

What values represents true and false in C? In Fortran?

Are there named Boolean constants in C?

What is a relational expression?