Rouge Documentation Help

Comparing values

In programming, it is often required to check whether two or more values are equal. To compare two values, you can use the equality operator (==), as seen in the following example. The comparison will result in a Boolean value, equaling true when both values are the same and false in case they differ.

42 == 42 42 == 43 (42 / 2) == 21

Output:

true false true

We can also do the opposite and check two values for inequality by using the inequality operator (!=).

42 != 42 42 != 43

Output:

false true

All data types can be compared for equality. However, when two values of different data types are compared, the comparison will always yield false.

"hello" == "hello" true == true 42 == "42"

Output:

true true false

Comparing numbers

Numbers can be checked for equality like any other values. However, the following operators allow you to perform more precise comparisons. They are only available when comparing numbers.

Greater than

Checks whether the number on the left is greater than the one on the right.

42 > 41 41 > 41

Output:

true false
Smaller than

Checks whether the number on the left is smaller than the one on the right.

41 < 42 42 < 41

Output:

true false
Greater than or equal to

Checks whether the number on the left is greater than or equal to the one on the right.

42 >= 42 42 >= 41 42 >= 43

Output:

true true false
Smaller than or equal to

Checks whether the number on the left is smaller than or equal to the one on the right.

42 <= 42 41 <= 42 42 <= 41

Output:

true true false

Negating booleans

In some cases, it may be necessary to invert (negate) a boolean value. When a boolean value is equal to true and we apply the negation operator to it, it will become false. In case the boolean value is false, it will become true instead. In the following example we check whether a person is an adult based on their age. The age of the person is stored in the variable called age. First, we create a boolean variable called is_adult that equals true when the value stored in age is at least 18. The negation of checking whether the person is an adult is to check whether the person is a child. Instead of calculating a new result, we can simply negate the value of is_adult using the negation operator (!).

age = 48 is_adult = age >= 18 is_child = !is_adult is_child

Output:

false

The following table shows you which boolean values lead to which outputs when using the negation operator.

!a

a = true

false

a = false

true

Combining comparisons

We can also combine multiple boolean values, e.g. the results of multiple comparisons. First, let's take a look at the and-operator (&&) which requires both values to be true in order for the combination to yield true as well. In any other case, this operator results in false, as can be seen in the following table.

a && b

a = true, b = true

true

a = true, b = false

false

a = false, b = true

false

a = false, b = false

false

The operator is showcased in the following example where, for someone to be allowed to enter, their name has to be "Steve" and they have to be an adult. The parentheses in the example have been added to increase readability, even though they are not technically required.

name = "Steve" age = 42 allowed_to_enter = (name == "Steve") && (age >= 18) allowed_to_enter

Output:

true

The second operator for combining boolean values is called the or-operator (||), which requires only one of the values to be true in order for the result to be true as well. Take a look at the following table to see which combinations of values lead to which results.

a || b

a = true, b = true

true

a = true, b = false

true

a = false, b = true

true

a = false, b = false

false

The or-operator is applied in the following example where, to enter, the name can either be "Steve" or "Alex".

name = "Steve" allowed_to_enter = (name == "Steve") || (name == "Alex") allowed_to_enter

Output:

true

Operator precedence

Just like with mathematical operators, boolean operators are also applied in a certain order when they are used in combination. To begin with, parentheses have the highest precedence, meaning that all operations are performed in the order that is specified by them. Second, the combination operators (&&, ||) take action. Only at that point are the comparison operators (==, !=, <, <=, >, >=) evaluated from left to right, as they have the lowest priority.

Last modified: 07 January 2026