A Boolean expression is an expression that results in a Boolean value, that is, either true or false.
A simple Boolean expression is written as
Operand1 Comparison Operator Operand2
where
Operand1andOperand2can be values, variables or mathematical expressionsComparison Operatorcan be one of those shown in the next table
PHP
|
PHP Comparison Operator |
Description |
|
== | Equal (not assignment) |
|
!= | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Notice: A very common mistake that novice programmers make when writing PHP programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing
$x = 5when they actually want to say$x == 5.
Here are some examples of Boolean expressions in PHP:
$x == 5. This can be read as “test if$xis equal to 5”$x > $y. This can be read as “test if$xis greater than$y”$x <= $y. This can be read as “test if$xis less than or equal to$y”$x != 3 * $y + 4. This can be read as “test if$xis not equal to the result of the expression3 * $y + 4”$s == "Hello". This can be read as “test if$sis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
$a = $x > $y;
assigns a value of true or false to variable $a. It can be read as “If the content of variable $x is greater than the content of variable $y, assign the value true to variable $a; otherwise, assign the value false.” This next example displays the number 1 on the screen.
<?php
$x = 8;
$y = 5;
$a = $x > $y;
echo $a;
?>
Notice: Please note that in PHP, the Boolean value true can also be represented by the value of 1.
Java
|
Java Comparison Operator |
Description |
|
== | Equal (not assignment) |
|
!= | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Notice: A very common mistake that novice programmers make when writing Java programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing
x = 5when they actually want to sayx == 5.Remember: In Java, in order to test if two strings are lexicographically equal, you need to use the
equals()method. For example, the statementa.equals(b)tests if the content of the string variableais equal to the content of string variableb.Remember: In Java, in order to test if one string is lexicographically “greater” or “less” than another string, you need to use the
compareTo()method. For example, the statementa.compareTo(b)compares the content of the string variableato the content of string variableband returns a value greater than 0 if variableais lexicographically greater than variableb, or a value less than 0 if variableais lexicographically less than variableb.
Here are some examples of Boolean expressions in Java:
x == 5. This can be read as “test ifxis equal to 5”x > y. This can be read as “test ifxis greater thany”x <= y. This can be read as “test ifxis less than or equal toy”x != 3 * y + 4. This can be read as “test ifxis not equal to the result of the expression3 * y + 4”s.equals("Hello") == true. This can be read as “test ifsis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
a = x > y;
assigns a value of true or false to variable a. It can be read as “If the content of variable x is greater than the content of variable y, assign the value true to variable a; otherwise, assign the value false.” This next example displays the number true on the screen.
public static void main(String[] args) throws java.io.IOException {
int x, y;
boolean a;
x = 8;
y = 5;
a = x > y;
System.out.println(a);
}
C++
|
C++ Comparison Operator |
Description |
|
== | Equal (not assignment) |
|
!= | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Notice: A very common mistake that novice programmers make when writing C++ programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing
x = 5when they actually want to sayx == 5.
Here are some examples of Boolean expressions in C++:
x == 5. This can be read as “test ifxis equal to 5”x > y. This can be read as “test ifxis greater thany”x <= y. This can be read as “test ifxis less than or equal toy”x != 3 * y + 4. This can be read as “test ifxis not equal to the result of the expression3 * y + 4”s == "Hello". This can be read as “test ifsis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
a = x > y;
assigns a value of true or false to variable a. It can be read as “If the content of variable x is greater than the content of variable y, assign the value true to variable a; otherwise, assign the value false.” This next example displays the number true on the screen.
#include <iostream>
using namespace std;
int main() {
int x, y;
bool a;
x = 8;
y = 5;
a = x > y;
cout << a;
return 0;
}
C#
|
C# Comparison Operator |
Description |
|
== | Equal (not assignment) |
|
!= | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Notice: A very common mistake that novice programmers make when writing C# programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing
x = 5when they actually want to sayx == 5.Remember: In C#, in order to test if one string is lexicographically “greater” or “less” than another string, you need to use the
CompareTo() method. For example, the statementa.CompareTo(b)compares the content of the string variableato the content of string variableband returns a value greater than 0 if variableais lexicographically greater than variableb, or a value less than 0 if variableais lexicographically less than variableb.
Here are some examples of Boolean expressions in C#:
x == 5. This can be read as “test ifxis equal to 5”x > y. This can be read as “test ifxis greater thany”x <= y. This can be read as “test ifxis less than or equal toy”x != 3 * y + 4. This can be read as “test ifxis not equal to the result of the expression3 * y + 4”s == "Hello". This can be read as “test ifsis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
a = x > y;
assigns a value of true or false to variable a. It can be read as “If the content of variable x is greater than the content of variable y, assign the value true to variable a; otherwise, assign the value false.” This next example displays the number true on the screen.
static void Main() {
int x, y;
bool a;
x = 8;
y = 5;
a = x > y;
Console.Write(a);
Console.ReadKey();
}
Visual Basic
|
VB Comparison Operator |
Description |
|
= | Equal |
|
<> | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Remember: In Visual Basic, in order to test if one string is lexicographically “greater” or “less” than another string, you need to use the
CompareTo()procedure. For example, the statementa.CompareTo(b)compares the content of the string variableato the content of string variableband returns a value greater than 0 if variableais lexicographically greater than variableb, or a value less than 0 if variableais lexicographically less than variableb.
Here are some examples of Boolean expressions in Visual Basic:
x = 5. This can be read as “test ifxis equal to 5”x > y. This can be read as “test ifxis greater thany”x <= y. This can be read as “test ifxis less than or equal toy”x <> 3 * y + 4. This can be read as “test ifxis not equal to the result of the expression3 * y + 4”s = "Hello". This can be read as “test ifsis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
a = x > y
assigns a value of true or false to variable a. It can be read as “If the content of variable x is greater than the content of variable y, assign the value true to variable a; otherwise, assign the value false.” This next example displays the number true on the screen.
Sub Main()
Dim x, y As Integer
Dim a As Boolean
x = 8
y = 5
a = x > y
Console.Write(a)
Console.ReadKey()
End Sub
Python
|
Python Comparison Operator |
Description |
|
== | Equal (not assignment) |
|
!= | Not equal |
|
> | Greater than |
|
< | Less than |
|
>= | Greater than or equal to |
|
<= | Less than or equal to |
Notice: A very common mistake that novice programmers make when writing Python programs is to confuse the assignment operator with the equal operator. They frequently make the mistake of writing
x = 5when they actually want to sayx == 5.
Here are some examples of Boolean expressions in Python:
x == 5. This can be read as “test ifxis equal to 5”x > y. This can be read as “test ifxis greater thany”x <= y. This can be read as “test ifxis less than or equal toy”x != 3 * y + 4. This can be read as “test ifxis not equal to the result of the expression3 * y + 4”s = "Hello". This can be read as “test ifsis equal to the word ‘Hello’”
Notice: For humans, Boolean expressions should be interpreted as questions. They should be read as “Is something equal to/greater than/less than something else?” and the answer is just a “Yes” or a “No” (
trueorfalse).
Moreover, given that a Boolean expression actually returns a value (true or false), this value can be directly assigned to a variable. For example, the expression
a = x > y
assigns a value of true or false to variable a. It can be read as “If the content of variable x is greater than the content of variable y, assign the value true to variable a; otherwise, assign the value false.” This next example displays the number true on the screen.
x = 8
y = 5
a = x > y
print(a)