About Booleans and Comparison Operators - Images: Japanese
Hey there, everyone! Hope you're doing well.
Next up, let's cover booleans and comparison operators.
Booleans are also called truth values or logical values. The author tends to use boolean, but any of these terms works fine.
Also worth knowing: boolean comes from the name of mathematician George Boole, and you'll see this term used frequently in textbooks and on the job, so keep it in mind.
So let's start with 'booleans' — the type that gives you a clear-cut true or false answer. A boolean is a value that represents either 'true' or 'false'. Pretty straightforward.
Think of true and false as two fundamental built-in values — you can write them directly like this:
print(true) // Outputs the boolean 'true'. print(false) // Outputs the boolean 'false'.
One thing to watch out for: if you wrap them in quotes like "true", they become the strings 'true' and 'false' — not booleans. So don't use quotes if you want boolean values.
print("true") // This is the string 'true', not a boolean.
print("false") // This is the string 'false', not a boolean.
That's really the basics of booleans. Easy enough so far, right?
The key is how you use them. A boolean is simply the result of checking whether something is correct — true if it is, false if it isn't. But this turns out to be incredibly important in programming, and you'll be using booleans constantly.
The best way to understand booleans is to look at them alongside 'comparison operators', so let's go ahead and cover those too.
Comparison operators are operators that compare two values — such as the size of numbers or whether strings are equal — and return the appropriate boolean based on the result.
You've probably seen <, <=, >, and >= in math class — those are exactly what comparison operators are.
Let's try one out. Take a look at this example:
print(1 < 2)
This compares the number 1 and the number 2. Let's see what we get:
true
We get the boolean 'true'. Since 1 is indeed less than 2, the result is 'true' (correct). Now let's flip the symbol from '<' to '>':
print(1 > 2)
And we get:
false
This time it's 'false'. Since 1 is not greater than 2, the result is 'false' (incorrect). That's the basic idea — make sense so far?
Programming also gives you comparisons that don't exist in math. Here's a full list of comparison operators:
| == | Returns true if both operands are strictly equal. |
| != | Returns true if both operands are not strictly equal. |
| > | Returns true if the left operand is greater than the right operand. |
| >= | Returns true if the left operand is greater than or equal to the right operand. |
| < | Returns true if the left operand is less than the right operand. |
| <= | Returns true if the left operand is less than or equal to the right operand. |
For operands, check out this article.
The comparison operators in the table above should cover most of what you'll need.
Take another look at the description for == in the table above. It says "strictly equal" — let's talk about what that means.
The reason for the word "strictly" is that in some other languages, == doesn't always mean strict equality.
For example, take JavaScript — a language well known for being loosely typed. Here's a comparison between the string 1 and the number 1:
"1" == 1
Surprisingly, this returns true.
The string 1 and the number 1 clearly aren't the same thing — but JavaScript quietly converts one type to match the other before comparing, which leads to this odd behavior.
If you want strict comparison in JavaScript, you use this instead:
"1" === 1
Note the triple equals === instead of ==. This one correctly returns false.
Loose comparisons are a common source of bugs, so in JavaScript and PHP there's a convention: "use === instead of == unless you have a specific reason not to."
In Swift, == already performs strict comparison, so you can use == with confidence.
Also worth noting: in Swift, === is used for comparing objects and instances — not for strict value equality like in JavaScript. If you're coming from another language, be careful not to mix them up. We'll cover this in detail in a later article.
Alright, let's run through a few quick examples with comparison operators:
print(1 == 2) // false
print(1 != 2) // true
print(1 > 2) // false
print(1 >= 2) // false
print(1 < 2) // true
print(1 <= 2) // true
print("ワンパンマン" == "ワンパンマン") // true
print("ワンパンマン" == "ペルソナ5") // false
You might assume from your math background that comparison only works with numbers — but in programming, you can compare strings and other types too, so keep that in mind.
And that's a wrap for this one!
By seeing comparison operators in action, you should now have a solid feel for how booleans work as well. In the next article, we'll get into data types. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.