Professional Documents
Culture Documents
2 3 Boolean
2 3 Boolean
BOOLEANS
Emily B. Yuson
Booleans
• YES / NO
• ON / OFF
• TRUE / FALSE
#include <stdbool.h>
• A boolean variable is declared with
the bool keyword and can only take the
values true or false:
• Before trying to print the boolean variables,
you should know that boolean values are
returned as integers:
• 1 (or any other number that is not 0)
represents true
• 0 represents false
• Therefore, you must use the %d format specifier to print a boolean value:
Example:
• However, it is more common to return a boolean value by comparing values and variables.
Comparing Values and Variables
• From the example above, you can see that the return value is a boolean
value (1).
• You can also compare two variables:
Example:
• In the example below, we use the equal to (==) operator to
compare different values:
Example:
Real Life Example
• Let's think of a "real life example" where we need to find out if a person is
old enough to vote.
• In the example below, we use the >= comparison operator to find out if
the age (25) is greater than OR equal to the voting age limit, which is set
to 18:
Example:
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
• Example
• We can also test variables:
Example:
The else Statement