User Tools

Site Tools


relational-operators

Table of Contents

Relational Operators

Relational operators are used to compare two values. The result of the comparison is either “true” (non-zero) or “false” (zero). This result can then be used to make a decision regarding program flow.

SharpBASIC has the following relational operators:

operator     relation                      expression
--------     -----------------------       ----------
   ==        equality                      x == y
   <>        inequality                    x <> y
   <         less than                     x < y
   >         greater than                  x > y
   <=        less than or equal to         x <= y
   >=        greater than or equal to      x >= y

When arithmetic and relational operators are combined in one expression, the arithmetic operations are always done first. In the following example the expressions “x + y” and “(t - 1) / z” are done first and their results are then compared with the “less than” operator.

example

' SharpBASIC relational and arithmetic operation programming example
' ------------------------------------------------------------------
option strict;

incl "lib/sys.sbi";

dim x, y, t, z: int;
dim r: bool;

main do
  x = 10;
  y = 20;
  t = 81;
  z = 2;

  r = x + y < (t - 1) / z;
  print(r);
end
Output:

-1


Be careful using relational operators with real numbers. Calculations may give extremely close but not identical results. In particular, avoid testing for identity between two real values. For example, the print statement in the following if statement is not executed unless a is exactly equal to 0.0:

if a == 0.0 do
  print "exact result";
end
When a is an extremely small value, for example 1.0e-23, the print statement is not executed.

See also: logical operators, bitwise operators

relational-operators.txt · Last modified: 2023/06/28 10:15 by admin