Relational operators

A conditional statement uses conditional logic to determine what programming statements to execute. By using some conditional logic (a process of checking conditions), we determine if a certain condition is true or false. If the condition is true, we execute some part of the program. Otherwise, if the condition is false, we use some other part of the program.

To write a conditional statement, we need to use a relational operator supported in ASP. ASP also supports logical operators to create complex conditions.

Before you can construct conditional statements in ASP, you should first understand the relational operators available in ASP. The Relational operators are used for comparison of the value of one element with another. There are six types of relational operators: equal, greater than, less than, greater than or equal to, less than or equal to, and not equal to. Each of these operators can be used to compare the values of the variables. The result of each of these operators is either true or false. When using these operators, make sure all of the arguments are of same data type. Integers should be compared with integers, strings with strings, and so on. Table 1 reviews each of these operators.

Table 1 Relational operators
Operator Name Symbol Description
Equal = Returns true if both sides are equal.
Greater than > Returns true if the variable on the left is greater than the variable on the right.
Less than < Returns true if the variable on the left is less than the variable on the right.
Greater than or equal to >= Returns true if the variable on the left is greater than or equal to the value of the variable on the right
Less than or equal to <= Returns true if the variable on the left is less than or equal to the value of the variable on the right
Not equal to <> Returns true if both sides are not equal.

Table 2 shows truth table with examples for each of the relational operator. As the table shows, the A is set to 20 and B is set to 15. When we check equality with the equal operator, both sides must be equal or same for the expression to be true. Because the 20 is not equal to 15, the A = B is false. In the second example in the table, we check if A > B. In this case, the expression is true because 20 is greater than 15. Using similar logic, the table shows results for other relation operators.

Table 2 truth table for relational operators
Operator Symbol Example (Assume A = 20, B = 15) True when False when Result
= A = B A and B are same or equal A and B are different False
> A > B A is greater than B A is not greater than B True
< A < B A is less than B A is not less than B False
>= A >= B A is greater than or equal to B B is greater than A True
<= A <= B A is less than or equal to B B is less than A False
<> A <> B A is not equal to B A and B are same or equal True