float s. In the following example, our
temperature conversion function is rewritten to use
float s:
Notice how we have added .0 to the end of our constants. This ensures that the compiler
knows to treat them as
float s rather than
int s.
boolean Boolean values are logical. They have a value that is either true or false.
In the C language,
Boolean is spelled with a lowercase
b, but in general use,
Boolean has an uppercase initial letter, as it is named after the mathematician George Boole, who
invented the Boolean logic that is crucial to computer science.
You may not realize it, but you have already met Boolean values when we were looking
at the
if command. The condition in an
if statement, such as
(count == 20) , is actually an
expression that yields a
boolean result. The operator
== is called a comparison operator.
Whereas
+ is an arithmetic operator that adds two numbers together,
== is a comparison
operator that compares two numbers and returns a value of either true or false.
You can define Boolean variables and use them as follows:
Boolean values can be manipulated using Boolean operators. So, similar to how you can
perform arithmetic on numbers, you can also perform operations on Boolean values. The
most commonly used Boolean operators are
and , which is written as
&& , and
or , which
is written as
|| .
Figure 4-1
shows truth tables for the
and and
or operators.
From the truth tables in
Figure 4-1
, you can see that for
and , if both A and B are true,
then the result will be true; otherwise, the result will be false.
On the other hand, with the
or operator, if either A or B or both A and B are true, then
the result will be true. The result will be false only if neither A nor B is true.
In addition to