SystemVerilog Constraint implication if else

 Implication (->


The implication operator ( –> ) can be used to declaring conditional relations.
      expression -> constraint
if the expression is true,then the constraints must be satisfied

 Example-1: 


In the example below,
     if addr_range == small, addr will get value less than 8.

class packet;
  rand bit [3:0] addr;
       string    addr_range;
  constraint address_range { (addr_range == "small") -> (addr < 8);}
endclass

module constr_implication;
  initial begin
    packet pkt;
    pkt = new();

    pkt.addr_range = "small";
    $display("------------------------------------");
    repeat(4) begin
      pkt.randomize();
      $display("\taddr_range = %s addr = %0d",pkt.addr_range,pkt.addr);
    end
    $display("------------------------------------");
  end
endmodule

 Simulator Output  
------------------------------------
addr_range = small addr = 6
addr_range = small addr = 2
addr_range = small addr = 1
addr_range = small addr = 4
------------------------------------

Execute the above code on 

if...else 


If the expression is true, all of the constraints in the first constraint/constraint-block must be satisfied, otherwise all of the constraints in the optional else constraint/constraint-block must be satisfied.

 Example-1: 


In the example below,
     if addr_range == small, addr will get value less than 8 else addr will get value greater than 8.

class packet;
  rand bit [3:0] addr;
       string    addr_range;

  constraint address_range { if(addr_range == "small")
                                addr < 8;
                             else
                                addr > 8;
                           }
endclass

module constr_if_else;
  initial begin
    packet pkt;
    pkt = new();
    pkt.addr_range = "small";
    $display("------------------------------------");
    repeat(3) begin
      pkt.randomize();
      $display("\taddr_range = %s addr = %0d",pkt.addr_range,pkt.addr);
    end
    $display("------------------------------------");

    pkt.addr_range = "high";
    $display("------------------------------------");
    repeat(3) begin
      pkt.randomize();
      $display("\taddr_range = %s addr = %0d",pkt.addr_range,pkt.addr);
    end
    $display("------------------------------------");
  end
endmodule

 Simulator Output  
------------------------------------
addr_range = small addr = 1
addr_range = small addr = 4
addr_range = small addr = 6
------------------------------------
------------------------------------
addr_range = high addr = 12
addr_range = high addr = 15
addr_range = high addr = 9
------------------------------------
Execute the above code on