4.4. Booleans¶
So far, we have seen that we can store integers and decimals in our variables. Sweet! However, this is not all we can store in our variables. Today, we are going to discuss a new value type called booleans. Ignore the weird sounding name! These values are really useful when programming because they allow our computers to communicate the idea of something being true or false. In my opinion, the boolean is one of the most powerful data types that we can use in our programs because it allows our computers to make decisions!
Like I said, booleans are the values true and false. That’s it! These two little words make up this super awesome data type. If we wanted to declare a variable that stores a boolean value, we might write something like this:
1. boolean answer;
2. boolean result;
Here is how our computer would think about these lines of code:
Notice that the type of these variables is boolean
. What if we wanted to initialize these variables to boolean values? Well, it might looks something like this:
/* Declare boolean variables */
1. boolean answer;
2. boolean result;
/* Initialize variables to boolean values */
3. answer = true;
4. result = false;
Our computer would think about this program like so:
Need to declare and initialize a boolean variable all in a single line? That will look something like this:
1. boolean gameWon = false;
TL;DR
A boolean value can be either true or false. That’s it!
When declaring boolean variables, you must use the keyword
boolean
for its type.