Relational Operators

Keysymbols: >, <, >=, <=.

The relational operators are less than, more than, less then or equal to and more than or equal to. The true and false values are defined in the same way as above in the logical operator. In this case if any operand is unknown the whole expression evaluates to unknown. See examples below.

        module relatTest;
           reg [3:0] a, b ,c, d;

           initial begin
              a=2;
              b=5;
              c=2;
              d=4'hx;

              $display(a < b);  // LHS less than RHS, evaluates to true, 1
              $display(a > b);  // LHS more than RHS, evaluates to false, 0
              $display(a >= c); // more than or equal to, evaluates to true, 1
              $display(d <= a); // less than or equal to, evaluates to unknown
            end
        endmodule // relatTest

EXERCISE
How many times will the following while loop execute ?
        module relatTest2;
           reg [3:0] a, b ,c, d;

           initial begin
              a = 1;
              b = 4;
              c = 0;
              while (b >= c)
                 if (b > a) c = c + 1;
              $display(c);
           end
        endmodule // relatTest2

Answers


previous next contents