The first time I heard the word “boolean” I didn’t even know what to associate it with. I guess the only thing that came to my mind was an image of someone shouting “boo” to scare me. But since I was doing a coding bootcamp at the time, I soon learned that they aren’t scary at all, in fact they’re pretty simple!
The term comes from a branch of algebra called Boolean algebra, after its founder, George Boole (who would have turned 200 years old last Monday!). It basically means that any variable can only have two values: “true” or “false” – which are represented by “1” or “0”. You’ve probably heard about it, these variables are also called binary.
Alright, so what is a boolean in programming? It is a true or false condition that can be used to create an algorithm. A boolean can check whether required conditions are satisfied and direct the program, depending on the outcome.
A lot of coding academies use some sort of an age example to illustrate how booleans work. I have created my own, so here it is! (look for the “if” statement)
It is very simple, if you press the “start” button, it will ask for your age. You will need to type in a value and the program will check whether it is below 18 or not and will return a message. I used the example of purchasing alcohol since that is one of the most popular application of filtering people by age.
It took me some time to figure out how to embed code into a WordPress post and I’ve finally managed it using the Code Embed plugin! But for some reason after you run the program, you get back to the home page and I don’t know how to change it 🙁 so please bear with me, I’m working on it! 🙂
So here’s the tiny program that will check if you’re over 18 (you can test different values):
Boolean logic goes further and includes Boolean operators (AND + OR), but I don’t want to go into details now because I’m not sure if I’ll be able to explain it well enough at this point 😉