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

  • Operand1 and Operand2 can be values, variables or mathematical expressions
  • Comparison Operator can 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 = 5 when 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 $x is equal to 5
  • $x > $y. This can be read as “test if $x is greater than $y
  • $x <= $y. This can be read as “test if $x is less than or equal to $y
  • $x != 3 * $y + 4. This can be read as “test if $x is not equal to the result of the expression 3 * $y + 4
  • $s == "Hello". This can be read as “test if $s is 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” (true or false).

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 = 5 when they actually want to say x == 5.

Remember: In Java, in order to test if two strings are lexicographically equal, you need to use the equals() method. For example, the statement a.equals(b) tests if the content of the string variable a is equal to the content of string variable b. 

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 statement a.compareTo(b) compares the content of the string variable a to the content of string variable b and returns a value greater than 0 if variable a is lexicographically greater than variable b, or a value less than 0 if variable a is lexicographically less than variable b.

Here are some examples of Boolean expressions in Java:

  • x == 5 . This can be read as “test if x is equal to 5
  • x > y. This can be read as “test if x is greater than y
  • x <= y. This can be read as “test if x is less than or equal to y
  • x != 3 * y + 4. This can be read as “test if x is not equal to the result of the expression 3 * y + 4
  • s.equals("Hello") == true. This can be read as “test if s is 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” (true or false).

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 = 5 when they actually want to say x == 5.

Here are some examples of Boolean expressions in C++:

  • x == 5 . This can be read as “test if x is equal to 5
  • x > y. This can be read as “test if x is greater than y
  • x <= y. This can be read as “test if x is less than or equal to y
  • x != 3 * y + 4. This can be read as “test if x is not equal to the result of the expression 3 * y + 4
  • s == "Hello". This can be read as “test if s is 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” (true or false).

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 = 5 when they actually want to say x == 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 statement a.CompareTo(b) compares the content of the string variable a to the content of string variable b and returns a value greater than 0 if variable a is lexicographically greater than variable b, or a value less than 0 if variable a is lexicographically less than variable b.

Here are some examples of Boolean expressions in C#:

  • x == 5 . This can be read as “test if x is equal to 5
  • x > y. This can be read as “test if x is greater than y
  • x <= y. This can be read as “test if x is less than or equal to y
  • x != 3 * y + 4. This can be read as “test if x is not equal to the result of the expression 3 * y + 4
  • s == "Hello". This can be read as “test if s is 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” (true or false).

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 statement a.CompareTo(b) compares the content of the string variable a to the content of string variable b and returns a value greater than 0 if variable a is lexicographically greater than variable b, or a value less than 0 if variable a is lexicographically less than variable b.

Here are some examples of Boolean expressions in Visual Basic:

  • x = 5 . This can be read as “test if x is equal to 5
  • x > y. This can be read as “test if x is greater than y
  • x <= y. This can be read as “test if x is less than or equal to y
  • x <> 3 * y + 4. This can be read as “test if x is not equal to the result of the expression 3 * y + 4
  • s = "Hello". This can be read as “test if s is 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” (true or false).

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 = 5 when they actually want to say x == 5.

Here are some examples of Boolean expressions in Python:

  • x == 5 . This can be read as “test if x is equal to 5
  • x > y. This can be read as “test if x is greater than y
  • x <= y. This can be read as “test if x is less than or equal to y
  • x != 3 * y + 4. This can be read as “test if x is not equal to the result of the expression 3 * y + 4
  • s = "Hello". This can be read as “test if s is 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” (true or false).

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)