Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

'if else if' synthesis

Verilog Conditional Statements

Example demonstrating how 'if else if' results in a priority encoder

Open in IDE
design.v
module des ( input [1:0] mode,
             input clk,
            input rstn,
            output reg [3:0] q);

  always @ (posedge clk) begin
    if (! rstn)
      q <= 0;
    else begin
      if (mode == 1)
        q <= q + 1;
      else if (mode == 2)
        q <= q - 1;
    end
  end
endmodule
testbench.v
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.