Lesson 5: Intro to Boolean Logic
You may remember from Lesson 2 (Data types) the "Boolean" data type. If you don't, that's okay; we didn't do much with them. But after this lesson, I promise: you will be on your way to becoming a boolean master. Money back guarantee! (yes, I do know you aren't paying anything for this...MUAHAHAHA!!! err, ahem..)
So, like I mentioned before, a boolean is either "true" or "false" in ruby. Really though, all a boolean is, is a representation of two possible values: true or false, on or off, 0 or 1, etc.
So, how are booleans useful?
Well, as you will learn, booleans and boolean logic is very useful in determining a program's state and can be used in decision making. However, before we get into that, I would like you to know how boolean logic works. I apologize that this lesson will be a little more conceptual than the previous lesson, but it is necessary you understand boolean logic before we begin the next lesson. If you can master boolean logic, you will be ahead of the game.
So, let's begin.
Now, we know that a boolean represents two values: true and false. But where do these values come from? First, we must look at boolean expressions, expression that generate a boolean value. You can think of a boolean expression, almost like a question, or more specifically: a boolean question.
A question could be:
"How are you today?"
"What is your name?"
These questions, however, could have many answers. A boolean question, would ask something more specific:
"Are you 18 years of age?"
"Do you own a computer?"
"Is your name Kellessdee?"
These questions, although in real life we may answer these in different ways, ultimately are looking for one answer, either "yes" or "no."
But how do we ask these kinds of questions in ruby?
Well, in programming, rather than looking at the question as a whole, you have to look at what data is really being tested.
"Is age equal to 18?"
"Is number of computers greater than 0?"
"Is name equal to 'Kellessdee'?"
While computers are terrible at interpreting human language, computers are REALLY good at comparing data or values. To write these comparisons into a program, the language in question will provide us with various comparative operators. We can create boolean expressions with these operators, which will evaluate to boolean values.
In ruby's case:
> - Greater than >= - Greater than or equal to < - Less than <= - Less than or equal to == - Equal to != - Not equal to
You can use these operators with nearly any object. Some of these may behave differently depending on the object, and for now I will only go over Numeric values and String values.
So back on those previous questions; how might we represent them in ruby?
irb(main):001 > age = 18 => 18 irb(main):002 > age == 18 => true irb(main):003 > number_of_computers = 0 => 0 irb(main):004 > number_of_computers > 0 => false irb(main):005 > name = 'kellessdee' => "kellessdee" irb(main):006 > name == 'Kellessdee' => false
So, as you can see, we use a variable to store the value, and we can compare that value based on the question we are asking.
irb(main):007 > "hello" == "hello" => true
However, you should never do this. There is no sense in using the computer's resources to compare values that are already known to the programmer. We will use variables, to simulate a value that could be any value
You might be wondering why the last expression evaluated to false. Well, as you may have known or guessed by now, when comparing strings, ruby compares each character and if all are equal, then the string is equal. Each character is actually represented by an integer (ASCII value), and thus, 'k' and 'K' are not the same.
Luckily, ruby strings have a useful method we can use to get around this - string.upcase and string.downcase
These methods convert each character in a string to it's upper-case or lower-case equivalent, and returns the new string. So, if you wanted to check the equality of two strings, but not depend on the case:
irb(main):008 > name = "KeLlEsSdEe" => "KeLlEsSdEe" irb(main):009 > name.downcase == "kellessdee" => true
or
irb(main):010 > name = "KELLESSDEE" => "KELLESSDEE" irb(main):011 > other = "kellessdee" => "kellessdee" irb(main):012 > name.downcase == other.downcase => true irb(main):013 > name.upcase == other.upcase => true
Another thing to remember about comparing strings, if you use the >, >=, <, <= to compare strings, ruby will compare the LENGTH (number of characters) of the two strings.
irb(main):017 > "one" < "three" => true
"one" is 3 characters, and "three" is 5 characters:
3 < 5 == true
A few other tips to keep in mind:
Be careful with > and >= or < and <=
when two values are compared with >= or <=, if they are equal, the expression will return true. With < or >, if they are equal the expression will return false.
So there we have it. We can now ask ruby simple questions, and receive boolean answers.
Now, for the hard part.
What if we want to ask ruby a more complex question?
"Is your name kellessdee and are you over 18?"
In reality, this is two questions. However, there is still only one answer. Yes or No. In my case, my name is kellessdee (well, username teehee) and I am 22, which means I am over 18. So, I would answer yes.
Easy enough for us to simply process...but what about a computer? Well, think about it this way. There are actually two individual questions, with individual answers:
name == kellessdee? Yes.
age > 18? Yes.
So really, the answer is Yes and yes. or true and true.
So, we could make two separate comparisons, and go from there, or we could use ruby's (conveniently) built-in "logical" operators. They are:
&& - logical AND || - logical OR ! - logical NOT and - logical AND or - logical OR not - logical NOT
So you have a couple options here to join simple boolean expressions, to form complex boolean expressions.
Most RPG Maker scripts use the English versions "and" "or" and "not". Because of this, and that these versions are easier to read, I will teach you with the English versions.
AND
and returns true if all expressions are true, otherwise returns false
irb(main):018 > true and true => true irb(main):019 > true and false => false irb(main):020 > false and false => false
OR
or returns true if one expression is true, and false only if all expressions are false
irb(main):021 > true or true => true irb(main):022 > true or false => true irb(main):023 > false or false => false
NOT
not is special, it is a unary operator (meaning it only operates on one value), and simply inverses the result of the expression or returns the opposite.
irb(main):024 > not true => false irb(main):025 > not false => true
So, lets try the complex question again:
irb(main):026 > name = "Kellessdee" => "Kellessdee" irb(main):027 > age = 22 => 22 irb(main):028 > (age > 18) and (name.downcase == "kellessdee") => true
And that, is the basics of boolean logic. Now, the problem with this, is it's one thing to explain and read about boolean logic, but it is harder to practice. It will be easier to understand once we apply it to decision-making, but for now, I would like to leave you with some practice questions (try to get the answer without putting these into IRB or a script).
1. true or (false and (true and false))
ANSWER
true
2. false and (true or false)
ANSWER
false
3. false or (true and false) or (true and (false or (true and (false or false))))
ANSWER
false
4. (true or (false and true) or false) and (true or (false and false))
ANSWER
true
5. (true and (true or false)) and (true or (true and (true and (false or false))) or true
ANSWER
true
Quick Technical Info on and/or
0 Comments
Recommended Comments
There are no comments to display.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now